Iterating through numbers using seq () and rep ()

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 

I usually just use the operator forin order to achieve this, but I'm limited to this use and can be used rep(), and seq()to achieve this vector.

+3
source share
2 answers
> 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
+6
source

One liner

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

Or even easier

rep(seq(5),each=5)+seq(5)-1
+1
source

All Articles