本文介绍了每组 R 尾随 cumsum的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要计算 R 中每组的运行 cumsum,但是 cumsum 的窗口只能是最后 3 个观察值:
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
推荐答案
你可以使用zoos rollapply 结合dplyrs group_by 和mutate:
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))
这篇关于每组 R 尾随 cumsum的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!