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
Normally I would just use a for
statement to achieve this but I am restricted from using that and can only use rep()
and seq()
to achieve this vector.
> 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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With