2012-11-30 34 views
1

我想设置一个xts对象的特定日期,但它有一天会改变日期。如何将确切的索引()分配给xts对象

aapl <- as.xts(read.zoo(textConnection(" 

    2007-04-26, 98.84 
    2007-04-27, 99.92 
    2007-04-30, 99.80 
    2007-05-01, 99.47 
    2007-05-02, 100.39"), sep=",")) 

    idx_aapl <- index(aapl) 

    idx_aapl 

    xts:::index.xts(aapl) # makes no difference 

    idx_aapl <- idx_aapl + 1 

    idx_aapl 

如何指定显示的具体日期?我已经阅读了posixct的一些内容,但我不知道如何将它分配给索引。

回答

1

您需要指定时区。例如。

aapl <- as.xts(read.zoo(textConnection(" 
    2007-04-26, 98.84 
    2007-04-27, 99.92 
    2007-04-30, 99.80 
    2007-05-01, 99.47 
    2007-05-02, 100.39"), sep=",", tz="UTC")) 

这是因为datestamps是POSIXct,这意味着它们也有一个时间组件。

解决它的另一种方法就是应用全球时区。例如。把它放在你现有脚本的顶部,也可以加载数据:

Sys.setenv(TZ = "UTC") 
library(xts) 
...