rsequenceshiftrep

How to shift integers by 1 but maintaining the same range?


Without using the c() function, I need to reproduce the following output:

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 have this for now but it does only repeat 1:5 5 times, while I need every repetition to be shifted 1 number

rep(seq(1, 5),5)  
#[1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5

cheers!


Solution

  • You're looking for

    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
    

    or using mapply.

    as.vector(mapply(`+`, list(1:5), 0:4))
    # [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