Generate a sequence which is an increasing part of

2019-07-11 11:08发布

问题:

First of all, sorry for the title. i tried to find an appropriate title.
My "problem" is that I have a vector, say 1-5 from which I would like to make a new vector. Which goes
1, 1,2 , 1,2,3, 1,2,3,4, 1,2,3,4,5

So for each round in includes one new number from the baseline vector until it reaches the end. I have a feeling it should be possible with seq...

回答1:

See ?sequence (from the manpage):

For each element of nvec the sequence seq_len(nvec[i]) is created. These are concatenated and the result returned.

example:

sequence(1:5)
[1] 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5