I have a data.table as follows
set.seed(5)
x <- data.table(x=sample(1:20,15))
> x
x
1: 5
2: 14
3: 17
4: 20
5: 2
6: 11
7: 8
8: 15
9: 12
10: 16
11: 3
12: 18
13: 10
14: 4
15: 13
and I would like to start at 1
and cumulate values iteratively such that the value of cumsum()
determines the next number to be added to the sum.
In the example I want to add the first value of x
, here 5, then jump to value number 5 and add that, here 2, then jump to value number 5+2=7
, here 8, then value number 5+2+8=15
, here 13.
That is, I want to get a vector
> res
[1] 1 5 7 15
Has anyone any idea for this problem?