问题
I need to compute the running cumsum per group in R but the window over which to cumsum must only be the last 3 observations:
If for example I have a table with a person's name, a date and a score as follow:
Name Date Score
1 John 2017-01-01 4
2 John 2017-01-02 5
3 John 2017-01-03 3
4 John 2017-01-04 1
5 John 2017-01-05 4
6 John 2017-01-06 4
7 Ben 2017-01-01 4
8 Ben 2017-01-02 4
9 Ben 2017-01-03 5
10 Ben 2017-01-04 2
11 Ben 2017-01-05 3
12 Ben 2017-01-06 4
13 Ben 2017-01-07 4
14 Ben 2017-01-08 4
I want to add a "custom cumsum" column which cumsums (per group) the last three days' scores, i.e. I want the following result:
Name Date Score Special_cum_sum
1 John 2017-01-01 4 4
2 John 2017-01-02 5 9
3 John 2017-01-03 3 12
4 John 2017-01-04 1 9
5 John 2017-01-05 4 8
6 John 2017-01-06 4 9
7 Ben 2017-01-01 4 4
8 Ben 2017-01-02 4 8
9 Ben 2017-01-03 5 13
10 Ben 2017-01-04 2 11
11 Ben 2017-01-05 3 10
12 Ben 2017-01-06 4 9
13 Ben 2017-01-07 4 11
14 Ben 2017-01-08 4 12
回答1:
You can use zoos rollapply combined with dplyrs group_by and mutate:
library(zoo)
library(dplyr)
?rollapply
Data <- Data %>% group_by(Name) %>%
mutate(Special_cum_sum = rollapply(Score, 3, sum, align = "right", partial = T))
来源:https://stackoverflow.com/questions/45050975/r-trailing-cumsum-per-group