I need to create the vector cx=(1, 1, 1, 2, 2, 2, 3, 3, 3)
. I have wrote a code.
k <- 3
tmp <- matrix(0, k, k)
tmp[1,] <-rep(c(1), times = k)
tmp[2,] <-rep(c(2), times = k)
tmp[3,] <-rep(c(3), times = k)
cx <-c(t(tmp))
cx
#[1] 1 1 1 2 2 2 3 3 3
Result is right for me but the code is very long.
How to rewrite code for arbitrary number k
?
My variant is:
k <- 3
tmp <- matrix(0, k, k)
for(i in 1:k) tmp[i,] <-rep(c(i), times = k)
cx <-c(t(tmp))