Skip to content

Instantly share code, notes, and snippets.

@druedin
Created Jun 15, 2013
Embed
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(is.na(x[(i-k):(i+k)])))))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment