Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Rolling means with zoo
doInstall <- TRUE
toInstall <- c("zoo")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
ftseIndex <- EuStockMarkets[, 4]
plot(ftseIndex, col = "GRAY")
# Calculate 10-day rolling mean, quickly:
smoothIndex <- rollmean(x = ftseIndex, # original series
k = 30, # width of the rolling window
fill = NA) # Pads head and/or tail with NA
length(ftseIndex) == length(smoothIndex)
lines(smoothIndex, col = "RED")
# If there are NA values in the original series, you'll need rollapply()
ftseIndex[c(40, 90, 300)] <- NA
smoothIndex2 <- rollapply(data = ftseIndex, # original series
width = 90, # width of the rolling window
FUN = mean, na.rm = T, # Any arbitrary function
fill = NA) # Padding
lines(smoothIndex2, col = "GREEN")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.