Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a sequence of sequences of different lengths

Tags:

r

sequence

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

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:

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 sequence will be

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

I can't do this with > 10,000 sequences which would be very inefficient. Is there any simpler way of doing this?

like image 904
user3664020 Avatar asked Dec 05 '15 14:12

user3664020


People also ask

Which function is used to generate sequences in R?

seq() function in R Language is used to create a sequence of elements in a Vector.

How do I generate a list of numbers in R?

How to Create Lists in R? We can use the list() function to create a list. Another way to create a list is to use the c() function. The c() function coerces elements into the same type, so, if there is a list amongst the elements, then all elements are turned into components of a list.

How do you ensure that sequences of different lengths are understood?

Question 6: If you have a number of sequences of different lengths, how do you ensure that they are understood when fed into a neural network? Make sure that they are all the same length using the pad_sequences method of the tokenizer Use the pad_sequences object from the tensorflow.keras.preprocessing.sequence namespace

How do you create a sequence in SQL?

Here is the basic syntax of the CREATE SEQUENCE statement: Specify the name of the sequence after the CREATE SEQUENCE keywords. If you want to create a sequence in a specific schema, you can specify the schema name in along with the sequence name. Specify the interval between sequence numbers after the INCREMENT BY keyword.

What is a sequence in math?

Background A sequencein mathematics, is a collection (like a set) of mathematical objects where the order of the objects is significant, and duplicate members of the collection are allowed. In computer science, we represent sequences as arrays, lists, streams, and a variety of other data structures.

How many ways to create sequences in Python?

7 Ways to Create Sequences in Python — Camden Reslink code.camdenreslink All Dev Data Math Misc May 12, 2018 7 Ways to Create Sequences in Python For, Iters, Recursion, Maps, Etc. Background


2 Answers

We can use sequence

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

data

length_v <- c(2,3,4,6)
like image 123
akrun Avatar answered Nov 15 '22 09:11

akrun


example:

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

so for you it will be:

unlist(sapply(length_v, seq, from=1))
like image 23
jogo Avatar answered Nov 15 '22 09:11

jogo