Home > OS >  Create sequences with specified lengths
Create sequences with specified lengths

Time:02-15

Is there a way to create sequences from 1 to each value of the vector a?

a <- c(1, 1, 1, 2, 1, 3, 2, 1, 1, 3)
b <- c(1, 1, 1, 1, 2, 1, 1, 2, 3, 1, 2, 1, 1, 1, 2, 3)

The values in a can be higher than 3.

CodePudding user response:

You can use sequence to get sequences with length described in a. sequence starts by default at 1 with an increment of 1.

sequence(a)
# [1] 1 1 1 1 2 1 1 2 3 1 2 1 1 1 2 3

all.equal(sequence(a), b)
# [1] TRUE
  • Related