1

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.

tonytonov
  • 25,060
  • 16
  • 82
  • 98
J D
  • 11
  • 1

1 Answers1

1

You need to specify timezones. E.g.

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. E.g. putting this at the top of your existing script, loads the data fine too:

Sys.setenv(TZ = "UTC")
library(xts)
...
Darren Cook
  • 27,837
  • 13
  • 117
  • 217