开发者

Repetitive vectors in R [duplicate]

开发者 https://www.devze.com 2023-03-22 02:23 出处:网络
This question already has an开发者_如何学Python answer here: Closed 11 years ago. Possible Duplicate:
This question already has an开发者_如何学Python answer here: Closed 11 years ago.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消