Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 225
Rene Avatar asked Nov 20 '25 00:11

Rene


1 Answers

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.

like image 131
Maël Avatar answered Nov 21 '25 15:11

Maël



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!