rmathvectorsequencerep

Create a sequence of sequences of numbers


I would like to make the following sequence in R, by using rep or any other function.

c(1, 2, 3, 4, 5, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5)

Basically, c(1:5, 2:5, 3:5, 4:5, 5:5).


Solution

  • Use sequence.

    sequence(5:1, from = 1:5)
    [1] 1 2 3 4 5 2 3 4 5 3 4 5 4 5 5
    

    The first argument, nvec, is the length of each sequence (5:1); the second, from, is the starting point for each sequence (1:5).

    Note: this works only for R >= 4.0.0. From R News 4.0.0:

    sequence() [...] gains arguments [e.g. from] to generate more complex sequences.