你几乎可以肯定那里有一个 NA ,然后通过rollmean()
. 正如这个小实验所示,一百万个值本身不会造成麻烦:
R> library(zoo)
R> X <- zoo(rnorm(1e6), order.by=Sys.time()+seq(1,1e6)*1e-3)
R> Xrm <- rollmean(X, 63)
R> summary(cbind(X, Xrm))
Index X Xrm
Min. :2012-04-11 13:57:28.9 Min. :-5.1857 Min. :-0.5843
1st Qu.:2012-04-11 14:01:38.9 1st Qu.:-0.6741 1st Qu.:-0.0866
Median :2012-04-11 14:05:48.9 Median :-0.0011 Median :-0.0013
Mean :2012-04-11 14:05:48.9 Mean :-0.0011 Mean :-0.0011
3rd Qu.:2012-04-11 14:09:58.9 3rd Qu.: 0.6727 3rd Qu.: 0.0838
Max. :2012-04-11 14:14:08.9 Max. : 4.8914 Max. : 0.5874
NA's :62
R>
这里唯一的 NA 是由于在开始时引入了初始滞后。
但是当我在 X 中引入一个单一的 NA 时,一切都崩溃了:
R> X[567890] <- NA
R> summary(cbind(X, rollmean(X, 63)))
Index X rollmean(X, 63)
Min. :2012-04-11 13:57:28.9 Min. :-5.18574 Min. :-1
1st Qu.:2012-04-11 14:01:38.9 1st Qu.:-0.67413 1st Qu.: 0
Median :2012-04-11 14:05:48.9 Median :-0.00110 Median : 0
Mean :2012-04-11 14:05:48.9 Mean :-0.00109 Mean : 0
3rd Qu.:2012-04-11 14:09:58.9 3rd Qu.: 0.67268 3rd Qu.: 0
Max. :2012-04-11 14:14:08.9 Max. : 4.89137 Max. : 1
NA's :1 NA's :432173
R>
我最终得到了 432k 的尾随 NA。