robintw
robintw

Reputation: 28511

Conversion from `zoo` to `xts` creates lots of NAs in the index

I have a rather strange problem that is probably best described by an example of an R session. I've tried to make this reproducible, as asked for in the comments below.

meto <- structure(c(30, 25, 25, 25, 20, 20, 20, 20, 20, 20), index = structure(c(12796, 
            12796.0416666667, 12796.0833333333, 12796.125, 12796.1666666667, 
            12796.2083333333, 12796.25, 12796.2916666667, 12796.3333333333, 
            12796.375), format = structure(c("d/m/y", "h:m:s"), .Names = c("dates", 
            "times")), origin = structure(c(1, 1, 1970), .Names = c("month", 
            "day", "year")), class = c("chron", "dates", "times")), class = "zoo")

The example dataset looks like:

> meto
(13/01/05 00:00:00) (13/01/05 01:00:00) (13/01/05 02:00:00) (13/01/05 03:00:00) (13/01/05 04:00:00) 
                 30                  25                  25                  25                  20 
(13/01/05 05:00:00) (13/01/05 06:00:00) (13/01/05 07:00:00) (13/01/05 08:00:00) (13/01/05 09:00:00) 
                 20                  20                  20                  20                  20 
> str(meto)
‘zoo’ series from (13/01/05 00:00:00) to (13/01/05 09:00:00)
  Data: num [1:10] 30 25 25 25 20 20 20 20 20 20
  Index: Classes 'chron', 'dates', 'times'  atomic [1:10] 12796 12796 12796 12796 12796 ...
  ..- attr(*, "format")= Named chr [1:2] "d/m/y" "h:m:s"
  .. ..- attr(*, "names")= chr [1:2] "dates" "times"
  ..- attr(*, "origin")= Named num [1:3] 1 1 1970
  .. ..- attr(*, "names")= chr [1:3] "month" "day" "year"

When we convert to XTS:

m <- as.xts(meto)

Which leads to the following output:

> str(m)
An ‘xts’ object from NA to NA containing:
  Data: num [1:10, 1] 30 25 25 25 20 20 20 20 20 20
  Indexed by objects of class: [chron,dates,times] TZ: 
  xts Attributes:  
 NULL
> summary(m)
     Index          m       
 Min.   :NA   Min.   :20.0  
 1st Qu.:NA   1st Qu.:20.0  
 Median :NA   Median :20.0  
 Mean   :NA   Mean   :22.5  
 3rd Qu.:NA   3rd Qu.:25.0  
 Max.   :NA   Max.   :30.0  
 NA's   :10                 
Warning message:
In data.row.names(row.names, rowsi, i) :
  some row.names duplicated: 2,3,4,5,6,7,8,9,10 --> row.names NOT used

As you can see, the zoo time series has a lot of data in it, indexed by chron objects. However, when I convert it to a xts time series using as.xts, it looks ok to begin with...but the str command shows NAs and comparing the summary of meto to m shows that over 36,000 NAs have been created in the index!

Does anyone have any idea why this is happening, or what I can do to solve it?

Upvotes: 3

Views: 2590

Answers (1)

Andrie
Andrie

Reputation: 179418

The problem is that your index is of class chron. I know very little about chron, but AFAIK it is generally preferred to use POSIX datetime objects in R, i.e. POSIXct or POSIXlt.

Somewhere in the conversion from zoo to xts the chron class information gets destroyed.

Converting your index to class POSIXct resolves the issue.

index(meto) <- as.POSIXct(index(meto)) 
as.xts(meto)

                    [,1]
2005-01-13 00:00:00   30
2005-01-13 01:00:00   25
2005-01-13 01:59:59   25
2005-01-13 03:00:00   25
2005-01-13 04:00:00   20
2005-01-13 04:59:59   20
2005-01-13 06:00:00   20
2005-01-13 07:00:00   20
2005-01-13 07:59:59   20
2005-01-13 09:00:00   20

For more information about working with R dates and time classes, see ?DateTimeClasses, ?POSIXct or ?strptime - which all leads to the same help page.


EDIT

If xts is supposed to handle chron objects when importing from zoo, you have probably found a bug in the function xts::xts.

The problem occurs in this line:

if (inherits(order.by, "dates")) 
    index <- as.numeric(as.POSIXct(strptime(as.character(order.by), 
        "(%m/%d/%y %H:%M:%S)")))

But notice that the format of your chron object is ("d/m/y", "h:m:s") - I know this from your str(meto). Look carefully - there is a misalignment between day and month.

This may well be a locale issue. I believe the package author lives in the USA, where the standard format is m/d/y, but in many other places the standard format is d/m/y.

So, somehow in the conversion between zoo and xts the conversion code should adjust for the locale of the user.

I suggest you contact the package author with this information.

Upvotes: 4

Related Questions