Re: [R] moving mean and moving variance functions

From: <>
Date: Mon, 04 Apr 2011 17:12:59 -0400

Thanks, I had recently seen reference to caTools but had forgotten about it.

Much appreciated.

-----Original message-----
From: Gabor Grothendieck <> To: Steve Friedman <> Cc:
Sent: Mon, Apr 4, 2011 13:17:36 GMT+00:00 Subject: Re: [R] moving mean and moving variance functions

On Mon, Apr 4, 2011 at 8:30 AM, Steve Friedman <> wrote:
> Hello
> Lets say as an example I have a dataframe with the following attributes:
> rownum(1:405), colnum(1:287), year(2000:2009), daily(rownum x colnum x year)
> and foragePotential (0:1, by 0.01).  The data is actually stored in a netcdf
> file and I'm trying to provide a conceptual version of the data.
> Ok. I need to calculate a moving mean and a moving variance for each cell on
> the following temporal
> windows - 7 day, 14 day, and 28 day. So far I have code for the moving
> average.
> ma <- function(x , n) {
>          filter(x, rep(1/n, n), sides = 1)
>      }   # note that when the function is used, n is defined for the
> temporal period (7, 14, and 28), and x is the input variable.
> ma7 <-   ma(dat, 7)  # where dat is accessing the foraging potential of the
> birds.
> ma14 <- ma(dat, 14)
> ma28 <- ma(dat, 28)
> This works fine.  What I don't have is the code for a moving variance.
> filter in the function above is included in the stats package and conducts a
> linear filtering on a Time Series.
> Is there comparable code some place in R for a moving variance?

See rollmean and rollapply in the zoo package and runmean and runsd in the caTools package.

Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at

	[[alternative HTML version deleted]]

______________________________________________ mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code.

Received on Mon 04 Apr 2011 - 21:15:15 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 04 Apr 2011 - 21:50:27 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive