Possible Duplicate:
R: generate a repeating sequence based on vector
To create the vector 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 is easy in one line, just type this into the command line and the appropriate output comes out immediately:
c(rep(1:3, 5))
But is there a similarly easy way to produce the vector 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 ?
The pattern of the repetition is different but it's not obvious to me why it's not amenable to a very simple solution. It's possible to do this with a "for" loop without too much difficulty, but can it be all compressed into one "line"?
You need the each
parameter within rep
:
> rep(1:5, each = 3)
[1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
精彩评论