Skip to content

Instantly share code, notes, and snippets.

Created June 15, 2013 20:34
What would you like to do?
Moving average handling NA
namav <- function(x,k=3){
x <- c(rep(NA, k),x,rep(NA,k)) # add NA on both sides
n <- length(x)
return(sapply((k+1):(n-k), function(i) sum(x[(i-k):(i+k)],na.rm=TRUE)/(2*k+1-sum([(i-k):(i+k)])))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment