Moving average handling NA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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