简体   繁体   中英

How to assign exact index() to xts object

I want to set the specific dates of an xts object but it shifts the dates one day.

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

How can I assign the specific dates shown? I have read something about posixct but I don't know how to assign it to the index.

You need to specify timezones. Eg

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"))

This is because the datestamps are POSIXct, which means they have a time component too.

The other way to fix it, is just to apply a global timezone. Eg putting this at the top of your existing script, loads the data fine too:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM