xm1
xm1

Reputation: 1765

rollmeanr issue with NA

for a base, let:

library(zoo)
x1=c(1,2,3,4)
x2=rollmeanr(x1,2,fill=NA)
x=cbind(x1,x2)
     x1  x2
[1,]  1  NA
[2,]  2 1.5
[3,]  3 2.5
[4,]  4 3.5

But if there is a NA in the base, rollmeanr does not work anymore:

x1[1]=NA
x3=rollmeanr(x1,2,fill=NA)
cbind(x1,x2,x3)
     x1  x2 x3
[1,] NA  NA NA
[2,]  2 1.5 NA
[3,]  3 2.5 NA
[4,]  4 3.5 NA

x3[3] should be 2.5 cause it has the 2 precedent data.
Is there a way to work around this?

Upvotes: 0

Views: 159

Answers (1)

r2evans
r2evans

Reputation: 160417

zoo changed the behavior of rollmean around version 1.8-2 (https://cran.r-project.org/web/packages/zoo/NEWS):

Changes in Version 1.8-2

  • The rollmean(x, k, ...) method now calls rollapply(x, k, (mean), ...) in case x contains any NAs (as the fast cumsum-based solution in rollmean is not applicable in this case). Analogously for rollsum() and rollmedian(). (Reported by Jan Gorecki.)

So the behavior you're seeing is in version 1.7; if you upgrade to 1.8 (as I'm guessing @phiver has done) then you should see your expected results.

Upvotes: 2

Related Questions