[R] Running mean in R?

Warnes, Gregory R gregory_r_warnes at groton.pfizer.com
Tue Mar 5 13:47:47 CET 2002

> From: Reto Burkard [mailto:burkard at giub.unibe.ch]
> ...
> Is there an easy function available in R for calculating the running
> mean?

If you just need the mean calculated over all previous elements of a vector,


Make sure and remove missing values first!

If, on the other hand, you want the mean calculated over a fixed number of
previous points use this function from the soon-to-be released version of
the gregmisc library:

############ running.R

"running" _ function( X, fun=mean, width=min(length(X),20),
  n _ length(X)

  from  <-  sapply( (1:n) - width + 1, function(x) max(x,1) )
  to    <-  1:n

  elements  <- apply(cbind(from,to), 1,function(x) seq(x[1], x[2]) )

    elements  <- as.data.frame(elements)

  funct _ function(which,what,fun,...) fun(what[which],...)

  Xvar _ sapply(elements, funct, what=X, fun=fun, ...)
  names(Xvar) <- paste(from,to,sep=":")

    Xvar[1:(width-1)]  <- NA


############# help page

running               package:gregmisc               R Documentation

Apply a Function Over Adjacent Subsets of a Vector


     Applies a function over subsets of the vector formed by taking a
     fixed number of previous points.


     running(X, fun=mean, width=min(length(X),20), allow.fewer=FALSE,...)


       X: data vector

     fun: function to apply. Default is `mean'

   width: integer giving the number of vector elements to include in
          the subsets.  Defaults to the lesser of the length of the
          data and 20 elements.

allow.fewer: Boolean indicating whether the function should be computed
          for initial subsets with fewer than `width' points

     ...: parameters to be passed to `fun'


     Vector containg the results of applying the function `fun' to the


     Gregory R. Warnes Gregory_R_Warnes\@groton.pfizer.com



     plot(1:20, running(1:20,width=5))
     plot(1:20, running(1:20,width=5, allow.fewer=T))

     # plot running mean and central 2 standard deviation range
     # estimated by last 40 observations
     dat <- rnorm(500, sd=1 + (1:500)/500 )
     fun <- function(x,sign=1) mean(x) + sign * sqrt(var(x))
     lines(running(dat,width=50,fun=fun, sign=-1, allow=T),col="red")
     lines(running(dat,width=50,fun=fun, sign=1, allow=T),col="red")

Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately.
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list