3

I've this kind of matrix.

I'm really sorry but I don't have a reproducible example.

Table 1 :

      [,1][,2][,3][,4][,5][,6][,7][,8][,9][,10]
[1,]    3   NA  NA  NA  NA  NA  NA  NA  NA  NA
[2,]    4   2   NA  NA  NA  NA  NA  NA  NA  NA
[3,]    4   1   7   NA  NA  NA  NA  NA  NA  NA
[4,]    4   1   2   3   NA  NA  NA  NA  NA  NA
[5,]    5   2   0   0   5   NA  NA  NA  NA  NA
[6,]    2   0   3   3   5   9   NA  NA  NA  NA
[7,]    6   2   0   0   3   4   2   NA  NA  NA
[8,]    12  6   4   3   1   0   2   6   NA  NA
[9,]    16  11  7   6   5   3   4   0   3   NA
[10,]   19  15  13  9   7   6   6   3   3   5

and I would like to create another one like this : Reconstruct column from diagonal.

Table 2:

      [,1][,2][,3][,4][,5][,6][,7][,8][,9]
[1,]    3   4   4   5   2   6   12  16  19
[2,]    2   1   1   0   2   6   11  15  NA
[3,]    7   2   0   0   4   7   13  NA  NA
[4,]    3   0   3   3   6   9   NA  NA  NA
[5,]    5   5   3   5   7   NA  NA  NA  NA
[6,]    9   4   0   6   NA  NA  NA  NA  NA
[7,]    2   2   4   NA  NA  NA  NA  NA  NA
[8,]    6   0   3   NA  NA  NA  NA  NA  NA
[9,]    3   3   NA  NA  NA  NA  NA  NA  NA
[10,]   5   NA  NA  NA  NA  NA  NA  NA  NA



Table 2[,1]= Table 1[1,1];[2,2];[3;3]
Table 2[,2]= Table 1 [2,1];[3,2];[4,3]
Table 2[,3]= Table 1 [3,1];[4,2];[5,3]

I tried this code, but without success.

Table2=matrix(NA, ncol=10, nrow=10)
for(i in 0:9)
{
  Table2[i+1]=Table1[i+1,i+1]
}

Next step, will be to compute a colMean. All this is for a cross validation to evaluate a sarima. Here it's just an example, in real data base, i've more than 100 col and 100 rows/

Thanks so much

user3355655
  • 463
  • 3
  • 15

2 Answers2

1

How about this?

t(apply(t(yr_mat), 1, function(x) c(na.omit(x), rep(NA, sum(is.na(x))))))

Taking the transpose of the matrix gets you this far:

    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
V1     3    4    4    4    5    2    6   12   16    19
V2    NA    2    1    1    2    0    2    6   11    15
V3    NA   NA    7    2    0    3    0    4    7    13
V4    NA   NA   NA    3    0    3    0    3    6     9
V5    NA   NA   NA   NA    5    5    3    1    5     7
V6    NA   NA   NA   NA   NA    9    4    0    3     6
V7    NA   NA   NA   NA   NA   NA    2    2    4     6
V8    NA   NA   NA   NA   NA   NA   NA    6    0     3
V9    NA   NA   NA   NA   NA   NA   NA   NA    3     3
V10   NA   NA   NA   NA   NA   NA   NA   NA   NA     5

So what you need is to 'slide over' all the rows, so the values are all the way left with trailing NAs. The function does this by truncating all NAs in each row, then adding back as many NAs after the good values.

Finally, you have to transpose all that again so it's oriented the way you want.

The output is:

    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
V1     3    4    4    4    5    2    6   12   16    19
V2     2    1    1    2    0    2    6   11   15    NA
V3     7    2    0    3    0    4    7   13   NA    NA
V4     3    0    3    0    3    6    9   NA   NA    NA
V5     5    5    3    1    5    7   NA   NA   NA    NA
V6     9    4    0    3    6   NA   NA   NA   NA    NA
V7     2    2    4    6   NA   NA   NA   NA   NA    NA
V8     6    0    3   NA   NA   NA   NA   NA   NA    NA
V9     3    3   NA   NA   NA   NA   NA   NA   NA    NA
V10    5   NA   NA   NA   NA   NA   NA   NA   NA    NA
arvi1000
  • 9,393
  • 2
  • 42
  • 52
0

Another option would be to create a numeric index ('indx') using the row and col of 'm1', then split the matrix ('m1') by 'indx', convert the 'list' to 'matrix' by stri_list2matrix from stringi. We get the output as character strings, that can be converted to 'numeric'.

library(stringi)
indx <- (row(m1)-col(m1)+1L)*(NA^upper.tri(m1))
matrix(as.numeric(stri_list2matrix(split(m1, indx))), dim(m1))
#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# [1,]    3    4    4    4    5    2    6   12   16    19
# [2,]    2    1    1    2    0    2    6   11   15    NA
# [3,]    7    2    0    3    0    4    7   13   NA    NA
# [4,]    3    0    3    0    3    6    9   NA   NA    NA
# [5,]    5    5    3    1    5    7   NA   NA   NA    NA
# [6,]    9    4    0    3    6   NA   NA   NA   NA    NA
# [7,]    2    2    4    6   NA   NA   NA   NA   NA    NA
# [8,]    6    0    3   NA   NA   NA   NA   NA   NA    NA
# [9,]    3    3   NA   NA   NA   NA   NA   NA   NA    NA
#[10,]    5   NA   NA   NA   NA   NA   NA   NA   NA    NA

data

m1 <- structure(c(3L, 4L, 4L, 4L, 5L, 2L, 6L, 12L, 16L, 19L, NA, 2L, 
1L, 1L, 2L, 0L, 2L, 6L, 11L, 15L, NA, NA, 7L, 2L, 0L, 3L, 0L, 
4L, 7L, 13L, NA, NA, NA, 3L, 0L, 3L, 0L, 3L, 6L, 9L, NA, NA, 
NA, NA, 5L, 5L, 3L, 1L, 5L, 7L, NA, NA, NA, NA, NA, 9L, 4L, 0L, 
3L, 6L, NA, NA, NA, NA, NA, NA, 2L, 2L, 4L, 6L, NA, NA, NA, NA, 
NA, NA, NA, 6L, 0L, 3L, NA, NA, NA, NA, NA, NA, NA, NA, 3L, 3L, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, 5L), .Dim = c(10L, 10L),
.Dimnames = list(NULL, NULL))
akrun
  • 874,273
  • 37
  • 540
  • 662