矩阵中的重复值 (R)

Repeat values in a matrix (R)

这是我正在使用的代码:

A <- matrix(1:9, nrow = 3)
A
cbind(A,A,A)

这给出了输出:

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,]    1    4    7    1    4    7    1    4    7
[2,]    2    5    8    2    5    8    2    5    8
[3,]    3    6    9    3    6    9    3    6    9

所需的输出是...

  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,]    1    1    1    4    4    4    7    7    7
[2,]    2    2    2    5    5    5    8    8    8
[3,]    3    3    3    6    6    6    9    9    9

另外我试过这个...

test <- (sapply(A , function(maybe) rep(maybe,each=3)))
test

输出为:

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,]    1    2    3    4    5    6    7    8    9
[2,]    1    2    3    4    5    6    7    8    9
[3,]    1    2    3    4    5    6    7    8    9

非常感谢您的帮助。

对列索引使用 repA[,rep(1:3, each=3)]