I have a data.table as follows -
library(data.table)
dt = data.table(
date = seq(as.Date("2015-12-01"), as.Date("2015-12-10"), by="days"),
v1 = seq(1, 10),
v2 = c(5, rep(NA, 9))
)
dt
date v1 v2
1: 2015-12-01 1 5
2: 2015-12-02 2 NA
3: 2015-12-03 3 NA
4: 2015-12-04 4 NA
5: 2015-12-05 5 NA
6: 2015-12-06 6 NA
7: 2015-12-07 7 NA
8: 2015-12-08 8 NA
9: 2015-12-09 9 NA
10: 2015-12-10 10 NA
I want to roll apply the function qma to the current row value of v1 and the previous row value of v2
qma <- function(x, y){(x+y+7)/2}
I am sure there must be a simple way to do this in one line using zoo::rollapplyr or data.table.
This is a follow-up question of the original one here R - Rolling sum of two columns in data.table