Rolling sum in dplyr

时光毁灭记忆、已成空白 提交于 2019-12-05 18:45:54

There is the rollify function in the tibbletime package that you could use. You can read about it in this vignette: Rolling calculations in tibbletime.

library(tibbletime)
library(dplyr)
rollig_sum <- rollify(.f = sum, window = 5)

df %>% 
  group_by(id) %>% 
  mutate(roll.sum = lag(rollig_sum(x))) #added lag() here
# A tibble: 20 x 3
# Groups:   id [2]
#       x    id roll.sum
#   <int> <int>    <int>
# 1     3     1       NA
# 2     8     1       NA
# 3     5     1       NA
# 4     9     1       NA
# 5    10     1       NA
# 6     1     1       35
# 7     6     1       33
# 8     9     1       31
# 9     6     1       35
#10     5     1       32
#11    10     2       NA
#12     5     2       NA
#13     7     2       NA
#14     6     2       NA
#15     2     2       NA
#16     9     2       30
#17     3     2       29
#18     1     2       27
#19     4     2       21
#20    10     2       19

If you want the NAs to be some other value, you can use, for example, if_else

df %>% 
  group_by(id) %>% 
  mutate(roll.sum = lag(rollig_sum(x))) %>%
  mutate(roll.sum = if_else(is.na(roll.sum), x, roll.sum))
 library(zoo)
 df %>%  group_by(id) %>%
       mutate(Sum_prev = rollapply(x, list(-(1:5)), sum, fill=NA, align = "right", partial=F))  

#you can use rollapply(x, list((1:5)), sum, fill=NA, align = "left", partial=F) 
#to sum the next 5 elements scaping the current one 


     x id Sum_prev
 1   3  1         NA
 2   8  1         NA
 3   5  1         NA
 4   9  1         NA
 5  10  1         NA
 6   1  1         35
 7   6  1         33
 8   9  1         31
 9   6  1         35
 10  5  1         32
 11 10  2         NA
 12  5  2         NA
 13  7  2         NA
 14  6  2         NA
 15  2  2         NA
 16  9  2         30
 17  3  2         29
 18  1  2         27
 19  4  2         21
 20 10  2         19
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!