I have a matrix
1 2
1 3
I want to duplicate each columns three times to create a matrix like this:
1 1 1 2 2 2
1 1 1 3 3 3
I dont think I can use rep
. Really appreciate any help
You can use rep in this situation, just not on the matrix itself. This does what you want:
mat1 = cbind(c(1,1), c(2,3))
mat2 = mat1[, rep(1:2, each=3)]
You can actually do it with a single rep
inside matrix
.
m <- matrix(c(1, 1, 2, 2), nrow = 2)
matrix(rep(as.numeric(t(m)), each = 3), nrow = nrow(m), byrow = TRUE)
Depending on the size of your matrix this might be quicker than using apply
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With