2012-11-30 32 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) 
...