-1

Suppose I have df1 like this:

Date                Var1
01/01/2015            1  
01/02/2015            4
....
07/24/2015            1
07/25/2015            6
07/26/2015            23
07/27/2015            15

Q1: Sum of Var1 on previous 3 days of 7/27/2015 (not including 7/27).

Q2: Sum of Var1 on previous 3 days of 7/25/2015 (This is not last row), basically I choose anyday as reference day, and then calculate rolling sum.

Jaap
  • 81,064
  • 34
  • 182
  • 193
azCats
  • 153
  • 13

1 Answers1

0

As suggested in one of the comments in the link referenced by @SeñorO, with a little bit of work you can use zoo::rollsum:

library(zoo)
set.seed(42)
df <- data.frame(d=seq.POSIXt(as.POSIXct('2015-01-01'), as.POSIXct('2015-02-14'), by='days'),
                 x=sample(20, size=45, replace=T))
k <- 3
df$sum3 <- c(0, cumsum(df$x[1:(k-1)]),
             head(zoo::rollsum(df$x, k=k), n=-1))
df
##             d  x sum3
## 1  2015-01-01 16    0
## 2  2015-01-02 12   16
## 3  2015-01-03 15   28
## 4  2015-01-04 15   43
## 5  2015-01-05 17   42
## 6  2015-01-06 10   47
## 7  2015-01-07 11   42

The 0, cumsum(...) is to pre-populate the first two rows that are ignored (rollsum(x, k) returns a vector of length length(x)-k+1). The head(..., n=-1) discards the last element, because you said that the nth entry should sum the previous 3 and not its own row.

r2evans
  • 141,215
  • 6
  • 77
  • 149