R: cumulative sum over rolling date range

后端 未结 3 1101
庸人自扰
庸人自扰 2020-12-14 12:21

In R, how can I calculate cumsum for a defined time period prior to the row being calculate? Prefer dplyr if possible.

For example, if the period was 10 days, then

3条回答
  •  自闭症患者
    2020-12-14 13:07

    I recommend using runner package designed to calculate functions on rolling/running windows. You can achieve this by using sum_run - one liner here:

    library(runner)
    library(dplyr)
    
    df %>%
      mutate(
        cum_rolling_10 = sum_run(
          x = df$value, 
          k = 10, 
          idx = as.Date(df$date, format = "%d/%m/%Y"))
      )
    
    
    df
    
    #          date value cum_rolling_10
    # 1   1/01/2000     9              9
    # 2   2/01/2000     1             10
    # 3   5/01/2000     9             19
    # 4   6/01/2000     3             22
    # 5   7/01/2000     4             26
    # 6   8/01/2000     3             29
    # 7  13/01/2000    10             29
    # 8  14/01/2000     9             38
    # 9  18/01/2000     2             21
    # 10 19/01/2000     9             30
    # 11 21/01/2000     8             38
    # 12 25/01/2000     5             24
    # 13 26/01/2000     1             25
    # 14 30/01/2000     6             20
    # 15 31/01/2000     6             18
    

    Enjoy!

提交回复
热议问题