Create a sequence of sequences of different lengths

I need to create a sequence of a large number (> 10000) of sequences of different lengths. I only know the lengths of these sequences in vector form.

length_v <- c(2,3,4,4,2,6,11,75...................)

Each sequence starts with 1 and moves forward with step 1. And in the final sequence (combined), each sequence should appear one after another, they cannot be mixed up.

Below is a small demo:

I say 4 sequences of length 2, 3, 4, 6, respectively.

s1 <- seq(1, 2)  # 1,2
s2 <- seq(1, 3)  # 1,2,3
s3 <- seq(1, 4)  # 1,2,3,4
s4 <- seq(1, 6)  # 1,2,3,4,5,6 

Final sequence will be

final <- c(s1,s2,s3,s4) **# the order has to be this only. No compromise here.**

I cannot do this s> 10,000 sequences, which would be very inefficient. Is there an easier way to do this?

+4
2

sequence

sequence(length_v)
#[1] 1 2 1 2 3 1 2 3 4 1 2 3 4 5 6

length_v <- c(2,3,4,6)
+5

:

unlist(sapply(c(2,3,4,6), seq, from=1))

:

unlist(sapply(length_v, seq, from=1))
+4

Source: https://habr.com/ru/post/1618706/


All Articles