创建不同长度的序列序列 [英] Create a sequence of sequences of different lengths
问题描述
我必须创建大量(> 10,000)不同长度的序列.我只知道向量形式的这些序列的长度.
I have to create a sequence of large number (> 10,000) of sequences of different lengths. I only know the lengths of these sequences in a vector form.
length_v <- c(2,3,4,4,2,6,11,75...................)
每个序列从1开始,以1为单位向前移动.最后一个序列(组合一个),每个序列都必须一个接一个出现,不能混在一起.
Each sequence starts from 1 and moves forward in steps of 1. And in the final sequence (combined one), each sequence has to appear one after the other, they can't be jumbled up.
下面是一个小的演示示例:
A small demonstrating example is below:
我已经说了 4 个长度分别为 2、3、4、6 的序列.
I have 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 <- c(s1,s2,s3,s4) **# the order has to be this only. No compromise here.**
我无法使用 > 10,000 个序列来执行此操作,这会非常低效.有没有更简单的方法?
I can't do this with > 10,000 sequences which would be very inefficient. Is there any simpler way of doing this?
推荐答案
我们可以使用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)
这篇关于创建不同长度的序列序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!