Reputation: 45
I have a matrix like this:
m1 <- matrix(c(1,2,3,4,5,6,7,8,9), nrow = 3, byrow = TRUE)
and I would like to have every column repeated "m" times, but transposing into files and concat the results horizontally. I mean, suppose "m" is 3, I would like to have something like this:
matrix(c(1,4,7,2,5,8,3,6,9,1,4,7,2,5,8,3,6,9,1,4,7,2,5,8,3,6,9),
nrow = 3, byrow = TRUE)
Is there any vectorized way to do this?
I have tried using rep to replicate the columns and then transposing, but I end with many rows
Upvotes: 3
Views: 162
Reputation: 887431
We can use rep
matrix(rep(m1, each=nrow(m1)), nrow=3)
Or
`dim<-`(rep(m1, each=nrow(m1)), dim(m1)*c(1,3))
Or
t(replicate(nrow(m1), c(m1)))
m1 <- matrix(c(1,2,3,4,5,6,7,8,9), nrow = 3, byrow = TRUE)
Upvotes: 2