I have the following dataset:
date <- StockData[1:10,2]
x <- c(2,4,2,3,5,1,6,2,3,4)
df <- as.data.frame(cbind(x,date))
df
x date
1 2 2019-06-28
2 4 2019-07-01
3 2 2019-07-02
4 3 2019-07-03
5 5 2019-07-04
6 1 2019-07-05
7 6 2019-07-08
8 2 2019-07-09
9 3 2019-07-10
10 4 2019-07-11
Now I want to create a new column with the 5 (n) days ahead rolling average, so the dataset looks as following:
x date five.day.average
1 2 2019-06-28 3.2
2 4 2019-07-01 3
3 2 2019-07-02 3.4
4 3 2019-07-03 3.4
5 5 2019-07-04 3.4
6 1 2019-07-05 3.2
7 6 2019-07-08 NA
8 2 2019-07-09 NA
9 3 2019-07-10 NA
10 4 2019-07-11 NA
In other words, I want a formula/function that does the same thing as rollmean in package zoo, but instead of back in time, future in time ( since k in rollmean cannot be negative).
Thank you in advance!