robintw
robintw

Reputation: 28511

One index value becomes NA when zoo time series converted to xts

I have a zoo time series:

z <- structure(c(55282, 55282, 55282, 55283, 55283, 55283, 55283, 
55283, 55283, 55283, 55283, 2339.96, 2331.98, 2335.53, 2340.33, 
2340.98, 2346.26, 2349.26, 2350.1, 2353.18, 2361.2, 2358.65, 
63.3, 54.5, 58.1, 62.9, 63.7, 69.3, 73.2, 74.5, 77.8, 86.3, 84.2, 
9.8, 8.4, 9, 9.7, 9.8, 10.6, 11.2, 11.5, 12, 13.3, 13), .Dim = c(11L, 
4L), .Dimnames = list(NULL, c("station_id", "ztd", "zwd", "iwv"
)), index = structure(c(14695.875, 14695.9166666667, 14695.9583333333, 
14696, 14696.0416666667, 14696.0833333333, 14696.125, 14696.1666666667, 
14696.2083333333, 14696.25, 14696.2916666667), format = structure(c("m/d/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")

Looking at the structure it (as far as I can see) looks fine:

> str(z)
‘zoo’ series from (03/27/10 21:00:00) to (03/28/10 07:00:00)
  Data: num [1:11, 1:4] 55282 55282 55282 55283 55283 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:4] "station_id" "ztd" "zwd" "iwv"
  Index: Classes 'chron', 'dates', 'times'  atomic [1:11] 14696 14696 14696 14696 14696 ...
  ..- attr(*, "format")= Named chr [1:2] "m/d/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"

and the data is all ok:

> z
                    station_id     ztd  zwd  iwv
(03/27/10 21:00:00)      55282 2339.96 63.3  9.8
(03/27/10 22:00:00)      55282 2331.98 54.5  8.4
(03/27/10 23:00:00)      55282 2335.53 58.1  9.0
(03/28/10 00:00:00)      55283 2340.33 62.9  9.7
(03/28/10 01:00:00)      55283 2340.98 63.7  9.8
(03/28/10 02:00:00)      55283 2346.26 69.3 10.6
(03/28/10 03:00:00)      55283 2349.26 73.2 11.2
(03/28/10 04:00:00)      55283 2350.10 74.5 11.5
(03/28/10 05:00:00)      55283 2353.18 77.8 12.0
(03/28/10 06:00:00)      55283 2361.20 86.3 13.3
(03/28/10 07:00:00)      55283 2358.65 84.2 13.0

However, when I convert the data to an xts time-series, one of the indices - and only one - gets changed to NA:

> x <- as.xts(z)
> x
                    station_id     ztd  zwd  iwv
(03/27/10 21:00:00)      55282 2339.96 63.3  9.8
(03/27/10 22:00:00)      55282 2331.98 54.5  8.4
(03/27/10 23:00:00)      55282 2335.53 58.1  9.0
(03/28/10 00:00:00)      55283 2340.33 62.9  9.7
(NA NA)                  55283 2340.98 63.7  9.8
(03/28/10 02:00:00)      55283 2346.26 69.3 10.6
(03/28/10 03:00:00)      55283 2349.26 73.2 11.2
(03/28/10 04:00:00)      55283 2350.10 74.5 11.5
(03/28/10 05:00:00)      55283 2353.18 77.8 12.0
(03/28/10 06:00:00)      55283 2361.20 86.3 13.3
(03/28/10 07:00:00)      55283 2358.65 84.2 13.0

It is the data for the 28th March 2010 at 01:00. I can't see why this is happening - does anyone have any ideas? I originally found this in a huge dataset (over 10 years of data) and it didn't happen for any other dates!

Upvotes: 2

Views: 202

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368201

These issues tend to all have the same origin: daylight savings time. It seems that the chron package leads to the entry being dropped.

But you could switch to POSIXct representation to avoid this shortcoming in chron:

R> zz <- xts(coredata(z), order.by=as.POSIXct(index(z)))
R> options("digits.secs"=0)   ## default display w/o microseconds
R> zz
                    station_id     ztd  zwd  iwv
2010-03-27 16:00:00      55282 2339.96 63.3  9.8
2010-03-27 17:00:00      55282 2331.98 54.5  8.4
2010-03-27 17:59:59      55282 2335.53 58.1  9.0
2010-03-27 19:00:00      55283 2340.33 62.9  9.7
2010-03-27 20:00:00      55283 2340.98 63.7  9.8
2010-03-27 20:59:59      55283 2346.26 69.3 10.6
2010-03-27 22:00:00      55283 2349.26 73.2 11.2
2010-03-27 23:00:00      55283 2350.10 74.5 11.5
2010-03-27 23:59:59      55283 2353.18 77.8 12.0
2010-03-28 01:00:00      55283 2361.20 86.3 13.3
2010-03-28 02:00:00      55283 2358.65 84.2 13.0
R> 

Looks like there are some rounding issues, or maybe your entries are off by a second

Upvotes: 7

Related Questions