'Use `dplyr` to sum all of the lagging values
I want to sum all of the lagging values in a group.
Using cars for example data;
#This returns what I want
for (i in 1:nrow(cars)) {
First<-which(cars$speed==cars[i,"speed"])[1]
cars[i,"DistSum"]<-sum(cars[First:i,"dist"], na.rm = T)
}
head(cars)
However, I do not want to use a for loop for this, and I would like the following to return what I want. If I could get lag to return all of the previous values in a group that should do the trick.
#dplyr version that does not work
cars <- (cars %>%
dplyr::group_by(speed) %>%
dplyr::mutate(DistSum = sum(lag(dist))) %>%
ungroup())
Another way of thinking of it might be that I need sum(lag(dist, n = ALL)).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
