3

New version of xts seems to have a different behavior when merging xts with different date class indices.

Here is a code example:

library(xts)
x1=xts(1:2,as.Date(c('1990-01-01','1991-01-01')))
x2=xts(3:4,as.POSIXct(c('1990-01-01','1991-01-01')))
merge(x1,x2)

Output using newest version 0.9-5.1 from r-forge:

           x1 x2
1990-01-01  1  NA
1990-01-01 NA  3
1991-01-01  2  NA
1991-01-01  NA 4

Same using version 0.8-6 version:

           x1 x2
1990-01-01  1  3
1991-01-01  2  4

Is there a way to force xts to convert the index to the same class before merging (as it was before) or the only way to make it work now is to force index class yourself before merging?

It would be great to have object attribute where you could specify what level of time precision you care about while working with it (aka ignore time if you work with daily data etc).

6
  • Thanks - noticed that you need to edit the tables before posting it here. Hope now the question looks readable Jul 22, 2013 at 17:20
  • The version of r-forge is the dev version, on CRAN you have the 0.9.5 and it works
    – dickoa
    Jul 22, 2013 at 18:04
  • 0.9.5 behaves in the same way as 0.9.5-1 (output is the same and issue is not fixed) Jul 22, 2013 at 18:16
  • Are you sure ? I wrote my output to show you
    – dickoa
    Jul 22, 2013 at 18:27
  • > sessionInfo() R version 2.15.3 (2013-03-01) Platform: x86_64-pc-linux-gnu (64-bit) [..SKIPPED..] other attached packages: [1] xts_0.9-5 zoo_1.7-10 produces the same output as original post. Which R version/platform are you on ? Jul 22, 2013 at 18:42

2 Answers 2

3

Edit: I'm not sure if the fact that it worked in 0.8-6 was a bug or desired behavior.

This is a timezone issue. Dates are considered to be midnight in UTC, so the index of x1 is in UTC. However, x2 has the timezone of your OS. The index of xts objects is always stored internally as POSIXct which is the number of seconds since the epoch in UTC. If your timezone is not UTC, then when the UTC Date is converted to POSIXct in the timezone of your OS, the times will not match up. One workaround would be to convert the index of one of them to match the other.

> # Convert the POSIXct index to Date
> index(x2) <- as.Date(index(x2))
> merge(x1,x2)
           x1 x2
1990-01-01  1  3
1991-01-01  2  4

> x2 <- xts(3:4,as.POSIXct(c('1990-01-01','1991-01-01')))
> # Convert the Date index to POSIXct
> index(x1) <- as.POSIXct(format(index(x1)))
> merge(x1, x2)
           x1 x2
1990-01-01  1  3
1991-01-01  2  4

I used format because as.POSIXct.Date does not make use of the tz argument.


In this example, it's possible to work around by setting a TZ environment variable first. Of course, that may have other implications depending on how you use timezones. And, it only works if you set the timezone variable before creating the xts object.

Sys.setenv(TZ="GMT")
x1=xts(1:2,as.Date(c('1990-01-01','1991-01-01')))
x2=xts(3:4,as.POSIXct(c('1990-01-01','1991-01-01')))
merge(x1, x2)
           x1 x2
1990-01-01  1  3
1991-01-01  2  4
1
  • Thanks -- was hoping that there was a work around that would not involve changing bunch of code in order to upgrade to newest xts version Jul 22, 2013 at 17:22
0

Not an answer, just to show OP that there's no problem with the CRAN version (at least for me)

library(xts)
x1=xts(1:2,as.Date(c('1990-01-01','1991-01-01')))
x2=xts(3:4,as.POSIXct(c('1990-01-01','1991-01-01')))

R> merge(x1,x2)
           x1 x2
1990-01-01  1  3
1991-01-01  2  4
R> packageVersion("xts")
[1] ‘0.9.5’
10
  • huh? are you sure you've got the current CRAN version?
    – GSee
    Jul 22, 2013 at 18:29
  • Did you set a timezone environment variable?
    – GSee
    Jul 22, 2013 at 18:31
  • No my timezone env is not set is not set and yes, if I recall correctly I downloaded from CRAN. I will redownload and test it to see
    – dickoa
    Jul 22, 2013 at 18:38
  • I can replicate it, still the same output. I started R in vanilla and uploaded zoo then xts and ran the same code and I have the same output
    – dickoa
    Jul 22, 2013 at 18:43
  • 1
    This works for you because you're UTC and xts objects with a Date class index have a UTC timezone. Jul 22, 2013 at 18:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.