9

I have a data set which has a couple of NA in it. I take a rolling mean and expect that when there is no NA in the window, the rolling mean should produce a number as opposed to NA, however, rollmeanr in zoo does not seem to do this. Example:

require(zoo)
z = zoo(cbind(a=0:10, b=c(NA,10:1), c=sample(1:11,11)), 1:11) 
rollmeanr(z, k=3, fill=NA)
    a  b        c
1  NA NA       NA
2  NA NA       NA
3   1 NA 3.333333
4   2 NA 4.666667
5   3 NA 4.000000
6   4 NA 6.333333
7   5 NA 7.000000
8   6 NA 9.333333
9   7 NA 8.333333
10  8 NA 8.666667
11  9 NA 5.666667

rollapply(z, width=3, FUN=mean, by=1, by.column=TRUE, fill=NA, align="right")
    a  b        c
1  NA NA       NA
2  NA NA       NA
3   1 NA 3.333333
4   2  9 4.666667
5   3  8 4.000000
6   4  7 6.333333
7   5  6 7.000000
8   6  5 9.333333
9   7  4 8.333333
10  8  3 8.666667
11  9  2 5.666667

I would expect these two calls to generate the same result. Please comment. Some session info:

sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=C                 LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
 [1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] zoo_1.7-10

loaded via a namespace (and not attached):
 [1] grid_3.0.1      lattice_0.20-15
4

3 回答 3

13

?rollmean

“rollmean”的默认方法不处理包含“NA”的输入。在这种情况下,请改用“rollapply”。

于 2013-07-20T18:07:55.087 回答
2

使用“部分=真”选项。该选项可以使用 NA 计算数据。

> rollapply(z, width=3, FUN=function(x) mean(x, na.rm=TRUE), by=1, by.column=TRUE, partial=TRUE, fill=NA, align="right")

     a    b        c
1  0.0  NaN 1.000000
2  0.5 10.0 5.500000
3  1.0  9.5 4.333333
4  2.0  9.0 6.666667
5  3.0  8.0 4.666667
6  4.0  7.0 6.000000
7  5.0  6.0 7.000000
8  6.0  5.0 8.666667
9  7.0  4.0 8.333333
10 8.0  3.0 7.000000
11 9.0  2.0 5.000000

如果要将第一行中的“NaN”更改为“0”,请将“fill=NA”修改为“fill=0”。

于 2017-03-06T04:19:24.283 回答
0

为了使其完整,rollsum 也无法处理包含“NA”的输入。在这种情况下,请改用“rollapply”。

于 2017-07-24T21:14:43.947 回答