Rolling means (and other functions) with zoo

December 2, 2012

(This article was first published on is.R(), and kindly contributed to R-bloggers)

The zoo package is designed for use with (potentially irregular) time series data. It is widely used for any number of applications, but among its most frequently useful functions are the roll* functions, such as rollmean, rollmedian, rollmax, rollapply, etc.

Today’s Gist shows you how to use these rolling functions to summarize time series data across a moving window. That is, you can calculate any function on a 5-day (or second, or year) basis, across the length of the entire vector. This is certainly something that could be done with a simple loop, but the roll* functions make it easy and fast.

To leave a comment for the author, please follow the link and comment on his blog: is.R(). offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.