[R] Parameterising apply To Compute Rolling Average of Columns in a matrix

Gabor Grothendieck ggrothendieck at gmail.com
Sun Nov 28 01:39:42 CET 2010


On Wed, Nov 17, 2010 at 2:27 PM, Paolo Rossi
<statmailinglists at googlemail.com> wrote:
> I sent a post to find a clever way to compute a Rolling Average of columns
> in a matrix  and I was given the solution below which I am very pleased
> with.
>

There is a zoo function that does this.  The following converts a to
zoo, performs the rolling average on columns and then converts back to
a matrix.  (A zoo object is just a matrix with a time index.)

library(zoo)
coredata(rollmean(as.zoo(a), 7))

Depending on what this is part of, you might be better off to keep it
as a zoo object since then you have access to all the other facilities
as well.

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



More information about the R-help mailing list