How to cumulatively multiply vector without using cumprod in r?
One option could be:
Reduce(`*`, x, accumulate = TRUE)
[1] 1 2 6
It doesn't use cumprod
...
x <- c(1,2,3)
exp(cumsum(log(x)))
#> [1] 1 2 6
Try this with a loop:
#Code
v1 <- c(1,2,3)
#Empty vector
v2 <- numeric(length(v1))
#Loop
for(i in 1:length(v1))
{
#Move around each element
e1 <- v1[1:i]
#Compute prod
vp <- prod(e1)
#Save
v2[i] <- vp
}
Output:
v2
[1] 1 2 6