Skip to content

Instantly share code, notes, and snippets.

@phil8192
Last active April 11, 2019 19:59
Show Gist options
  • Save phil8192/ade56e30d5a101b8eab50fa3a2f1af22 to your computer and use it in GitHub Desktop.
Save phil8192/ade56e30d5a101b8eab50fa3a2f1af22 to your computer and use it in GitHub Desktop.
roll apply with window of indices
wapply <- function(x, window=1:10, fun=mean) {
res <- rep(NA, length(x))
names(res) <- names(x)
i <- max(window) + 1
while(i <= length(x)) {
res[i] <- fun(x[i - window])
i <- i + 1
}
res
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment