Reputation: 189
I have a vector
df<- c(1000,1.02,1.03,1.04,1.01)
I want to create a new vector which contains cumulative products of the elements in 'df' (the old vector). The new column should look like
b<- c(1020,1050.6,1092.64,1103.5)
basically the first element of new vector is product of first two elements of old vectors. The second vector of the new element is product of first three elements of old vector and so on. I want to avoid loops.
Upvotes: 2
Views: 100
Reputation: 887203
We can use cumprod
cumprod(df)[-1]
#[1] 1020.000 1050.600 1092.624 1103.550
Upvotes: 1