R trailing cumsum per group

独自空忆成欢 提交于 2019-12-11 08:36:45

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!