Calculating the mean from matrix diagonals + increment
I have such a matrix.
I'm 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: Restore Column Diagonally.
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 have tried this code with no success.
Table2=matrix(NA, ncol=10, nrow=10)
for(i in 0:9)
{
Table2[i+1]=Table1[i+1,i+1]
}
The next step is to calculate the colMean. All of this is for cross-validation to evaluate the sarima. Here is just an example, in a real database, I have over 100 col and 100 rows /
Many thanks
source to share
How about this?
t(apply(t(yr_mat), 1, function(x) c(na.omit(x), rep(NA, sum(is.na(x))))))
Taking a transposed matrix leads you to this:
[,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, you need to flip all the lines so that the values remain unchanged with trailing NA. The function does this by truncating everything NA
on each line and then appending back as many NAs after the good values.
Finally, you need to rearrange the whole thing again so that it is oriented the way you want it to be.
Output:
[,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
source to share
Another option would be to create a numeric index ('indx') using row
and col
from 'm1', then split
matrix ('m1') to 'indx', convert 'enumerate' to 'matrix' to 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))
source to share