Iterate through numbers using seq() and rep()

前端 未结 2 1118
星月不相逢
星月不相逢 2021-01-14 02:47

I need to use rep() and seq() to get the following vector:

1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9 

No

相关标签:
2条回答
  • 2021-01-14 03:41

    One liner,

    do.call(c,sapply(1:5,seq,length.out=5,simplify=FALSE))
    

    Or even simpler,

    rep(seq(5),each=5)+seq(5)-1
    
    0 讨论(0)
  • 2021-01-14 03:46
    > 1:5 + rep(0:4, each=5)
     [1] 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9
    
    0 讨论(0)
提交回复
热议问题