sapply - Repeat values in a matrix (R) -


here code working with:

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

this gives output:

     [,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 

the desired output is...

  [,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 

in addition tried this...

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

which gives output of:

      [,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 

the appreciated.

use rep column indexing: a[,rep(1:3, each=3)]


Comments