We want to get an array that looks like this:
1,1,1,2,2,2,3,3,3,4,4,4,1,1,1,2,2,2,3,3,3,4,4,4,1,1,1,2,2,2,3,3,3,4,4,4
What is the easiest way to do it?
42-'s answer will work if your sequence of numbers incrementally increases by 1. However, if you want to include a sequence of numbers that increase by a set interval (e.g. from 0 to 60 by 15) you can do this:
rep(seq(0,60,15), times = 3)
[1] 0 15 30 45 60 0 15 30 45 60 0 15 30 45 60
You just have to change the number of times you want this to repeat.
Here is a method using array manipulation with aperm
. The idea is to construct an array containing the values. Rearrange them so they match the desired output using aperm
, and then "unfold" the array with c
.
c(aperm(array(1:4, dim=c(4,3,3)), c(2, 1, 3)))
[1] 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4