gpt4 book ai didi

r - na.approx 改变时区显示

转载 作者:行者123 更新时间:2023-12-02 01:58:45 25 4
gpt4 key购买 nike

我最近发现了 zoo 函数 na.approx 但想知道是否有一种方法可以在不改变时区显示方式的情况下使用它,如运行我的可重现示例所示...

set.seed(111)
x <- xts(matrix(cumprod(rnorm(100,0,0.001)+1)*100,
ncol=1,dimnames=list(rep("",100),c("JJ"))),
Sys.time()-c(100:1), tzone="America/Chicago")

x[30:50,] <- NA

tzone(x)
[1] "America/Chicago"
.index(head(x,1))
[1] 1377270598
attr(,"tzone")
[1] "America/Chicago"
attr(,"tclass")
[1] "POSIXct" "POSIXt"
head(x)
JJ
2013-08-23 10:09:57 100.02352
2013-08-23 10:09:58 99.99044
2013-08-23 10:09:59 99.95928
2013-08-23 10:10:00 99.72914
2013-08-23 10:10:01 99.71210
2013-08-23 10:10:02 99.72609
Warning message:
timezone of object (America/Chicago) is different than current timezone ().



y <- na.approx(x)

tzone(y)
[1] "America/Chicago"
.index(head(y,1))
[1] 1377270598
head(y)
JJ
2013-08-23 16:09:57 100.02352
2013-08-23 16:09:58 99.99044
2013-08-23 16:09:59 99.95928
2013-08-23 16:10:00 99.72914
2013-08-23 16:10:01 99.71210
2013-08-23 16:10:02 99.72609
Warning message:
timezone of object (America/Chicago) is different than current timezone ().

注意我不在芝加哥....并且我的 Sys.getenv('TZ') 未设置为 "America/Chicago"

最佳答案

编辑: 我可以用 xts 0.9-3 重现这个问题。它已在 xts 0.9-5 中修复。您需要更新 xts。


我相信这可以被认为是一个错误。

head(x)
# JJ
#2013-08-23 14:12:19 100.02352
#2013-08-23 14:12:20 99.99044
#2013-08-23 14:12:21 99.95928
#2013-08-23 14:12:22 99.72914
#2013-08-23 14:12:23 99.71210
#2013-08-23 14:12:24 99.72609

methods(na.approx)
#[1] na.approx.default na.approx.ts* na.approx.zoo* na.approx.zooreg*

所以,没有xts方法。由于

class(x)
#[1] "xts" "zoo"

na.approx.zoo被 dispatch 。在该函数内部,基本上会发生这种情况:

index(x) <- index(x)
head(x)
# JJ
#2013-08-23 21:12:19 100.02352
#2013-08-23 21:12:20 99.99044
#2013-08-23 21:12:21 99.95928
#2013-08-23 21:12:22 99.72914
#2013-08-23 21:12:23 99.71210
#2013-08-23 21:12:24 99.72609

请注意 index<-.xts存在并在这里使用。不幸的是,与index<-.zoo相反,它不保留(索引的)时区。

因此,您可以使用 y <- na.approx(as.zoo(x))来避免这个问题。

关于r - na.approx 改变时区显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18406150/

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