Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate cumulative product / multiple

Suppose I have a vector x as:

x <- c(X1, X2, X3, ..., Xn)

I want to write a function that automatically get the result series like this:

y <- c(X1, X1*X2, X1*X2*X3, ..., X1*X2*X3*...*Xn)

Can someone tell me how to do this in R?

like image 410
user2668789 Avatar asked Sep 02 '25 09:09

user2668789


1 Answers

Have a look at ?cumprod:

 cumprod(1:10)
 # [1]       1       2       6      24     120     720    5040   40320  362880 3628800
like image 165
sgibb Avatar answered Sep 04 '25 22:09

sgibb