4

I use rolling_sum to create a 3-month rolling sum for one column. However, it creates NA for rows with now() <3 while I'd like to see partial sum there. There is a Partial argument in the documentation, but says it is not currently implemented!

Here is my data and what I'd like to see:

df =
id   date   variable   3_month_sum_current   3-month_sum_needed
1    1      1          NA                    1
1    2      0          NA                    1
1    3      2          3                     3
1    4      4          6                     6

my code:

library(RcppRoll)
library(dplyr)
df <- df %>%group_by(id) %>% arrange(date) %>%
      mutate(3_month_sum_current = 
             roll_sum(variable, 3, fill=NA, align= 'right', partial = TRUE)
zx8754
  • 52,746
  • 12
  • 114
  • 209
Ana
  • 1,516
  • 3
  • 15
  • 26

1 Answers1

3

rollapplyr in the zoo package has a partial= argument.

zx8754
  • 52,746
  • 12
  • 114
  • 209