I feel like this questions should have been answered before, but I fail to find the answer. I want to do a very simple operation in R with a data frame: For each row in ascending order, I want to sum up all the values above (i.e. make a total up to this observation). However, using lag(), I fail to do this. Any suggestions?
Asked
Active
Viewed 439 times
1 Answers
0
As Andrew Gustar mentions, e.g.,
cars <- mtcars
cumcyl <- cumsum(cars$cyl)
cars <- cbind(cars, cumcyl)

SteveM
- 2,226
- 3
- 12
- 16
-
3or just `cars$cumcyl <- cumsum(cars$cyl)`, no need for the intermediate variable. – r2evans Oct 06 '20 at 20:05