public
Last active

Rolling means with zoo

  • Download Gist
advent_zoo.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
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")

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.