Home > OS >  How to create a sequence of sequences of numbers in R?
How to create a sequence of sequences of numbers in R?

Time:01-04

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

[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).

CodePudding user response:

Use sequence.

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

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

CodePudding user response:

unlist(lapply(1:5, function(i) i:5))
# [1] 1 2 3 4 5 2 3 4 5 3 4 5 4 5 5
  •  Tags:  
  • Related