Create a vector that repeats itself in R

前端 未结 1 1479
借酒劲吻你
借酒劲吻你 2020-12-12 08:21

I would like to create a vector that repeats itself. (eg 1:3 until 12 rows) 1,2,3,1,2,3,1,2,3,1,2,3

How can I do this in R?

Thanks for your help.

1条回答
  •  自闭症患者
    2020-12-12 08:37

    See ?rep. What you want is as easy as

    > rep(1:3, times = 4)
     [1] 1 2 3 1 2 3 1 2 3 1 2 3
    

    but if you don't know the length of the vector until run time but you do know the length of the output required, you could do (updated to reflect comment from @baptiste):

    > rep(1:3, length.out = 12)
     [1] 1 2 3 1 2 3 1 2 3 1 2 3
    

    0 讨论(0)
提交回复
热议问题