How do I provide multiple parameters and variables to an apply function? I'd like to pass vectir c(1, 2) to argument l and varaibles c(x, x1) to argument k.
Desired Output should have four columns instead of two
Example
library(zoo)
datt <- data.frame(x = rnorm(100),
x2 = rnorm(100))
mapply(function(l, k) c(rollmean(datt[,k], l), rep(NA, l-1)),
c(1, 2),
c("x", "x2"))
Output
x x2
1 -0.2688922 0.9506357
2 0.1402062 0.7324266
3 0.1391136 -1.7412695
4 1.4542990 2.1617833
5 -0.9409344 2.0495386
6 -0.8309520 0.6403896
lstatnds for the 'k' argument, there would be only two column output.