[R] rollapply.zoo() with na.rm=TRUE

Gabor Grothendieck ggrothendieck at gmail.com
Fri Aug 12 19:27:03 CEST 2011


On Fri, Aug 12, 2011 at 11:47 AM, Giles <giles.heywood at cantab.net> wrote:
> Hi.
>
> I'm comparing output from rollapply.zoo, as produced by two versions
> of R and package zoo.  I'm illustrating with an example from a R-help
> posting 'Zoo - bug ???' dated 2010-07-13.
>
> My question is not about the first version, or the questions raised in
> that posting, because the behaviour is as documented.  I'm puzzled as
> to why na.rm no longer is passed to mean, i.e. why element 2 is NA and
> not 1.5 when na.rm=TRUE, as it was before.
>
> The first example, where na.rm is not specified, and which now behaves
> more as one might expect prior to carefully reading the documentation,
> is also different from before.
>
> This is not specific to mean(), similar behaviour is shown for e.g. sum().
>
> Have I misunderstood the documentation?  Is there a way to reproduce
> the old behaviour with na.rm=TRUE?

This is a bug. Its fixed in the development version.

Get the entire development version or just that one file:

library(zoo)
source("http://r-forge.r-project.org/scm/viewvc.php/*checkout*/pkg/zoo/R/rollapply.R?root=zoo")
rollapply(a, FUN = mean, width = 3, na.rm = TRUE)

or use this workaround:
rollapply(a, FUN = function(x) mean(x, na.rm = TRUE),  width = 3)


-- 
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