gpt4 book ai didi

r - 缺少值时使用rollmean(NA)

转载 作者:行者123 更新时间:2023-12-04 05:52:35 27 4
gpt4 key购买 nike

我有一个包含几个NA的数据集。我采用滚动平均值,并期望当窗口中没有NA时,滚动平均值应产生一个与NA相反的数字,但是rollmeanr中的zoo似乎不执行此操作。例子:

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

我希望这两个调用会产生相同的结果。请给出意见。一些 session 信息:
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

最佳答案

来自?rollmean

The default method of ‘rollmean’ does not handle inputs that contain ‘NA’s. In such cases, use ‘rollapply’ instead.

关于r - 缺少值时使用rollmean(NA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17765001/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com