2013-08-23 54 views
3

我最近发现zoo函数na.approx,但想知道是否有一种方法可以在不改变时区显示方式的情况下使用它,如运行我的可重现示例所示。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(). 

NB我不是总部设在芝加哥....我Sys.getenv('TZ')未设置为"America/Chicago"

+0

你的'sessionInfo()'是什么? –

回答

2

编辑:我可以重现该问题与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被调度。这里面主要的功能发生这种情况:

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))来避免此问题。

+0

谢谢你。在提出问题之前,应该先更新'xts'并显示我的'sessionInfo()'。再次感谢 –