thevikas
thevikas

Reputation: 1639

how to iteratively sum of all values before the index till end of vector in R

I'm trying to find a simple function in R on how to iteratively sum a vector values and result in another vector.

Suppose I have a vector of [2,3,5,2,6,3]

I need a vector which sums and results as [2,5,10,12,18,21]

Upvotes: 0

Views: 268

Answers (1)

Pavlo
Pavlo

Reputation: 63

x <- c(2,3,5,2,6,3)
cumsum(x)
[1]  2  5 10 12 18 21

Here is your function, man.

Upvotes: 1

Related Questions