I'm new using the zoo package, so maybe it's an easy question. I have the following data frame (df):
library(lubridate)
library(zoo)
library(dplyr)
Date <- c("2010-01-28", "2010-01-28", "2010-02-28",
"2010-02-28", "2010-03-28", "2010-03-28",
"2010-04-28", "2010-04-28")
Date <- as_date(Date)
Amount <- 1:8
Prod <- c("Corn", "Potato","Corn", "Potato","Corn", "Potato","Corn", "Potato")
df <- data.frame(Date, Prod, Amount)
print(df)
Date Prod Amount
2010-01-28 Corn 1
2010-01-28 Potato 2
2010-02-28 Corn 3
2010-02-28 Potato 4
2010-03-28 Corn 5
2010-03-28 Potato 6
2010-04-28 Corn 7
2010-04-28 Potato 8
What I want is to calculate the rolling sum for each variable, with a "window" of 3 days, and then make a new data frame, equal as follows:
Date Prod Amount
2010-03-28 Corn 9
2010-03-28 Potato 12
2010-04-28 Corn 15
2010-04-28 Potato 18
Probably rollapply()
and dplyr could do the job, but I don't know how to resolve this.
I appreciate it if someone can help :)