2

我有一个动物园时间序列:

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

看看它的结构(据我所见)看起来不错:

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

数据一切正常:

> 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

但是,当我将数据转换为 xts 时间序列时,其中一个索引(也只有一个)更改为 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

这是 2010 年 3 月 28 日 01:00 的数据。我不明白为什么会这样 - 有人有什么想法吗?我最初在一个巨大的数据集(超过 10 年的数据)中发现了这一点,并且在任何其他日期都没有发生过!

4

1 回答 1

7

这些问题往往都有相同的根源:夏令时。似乎该chron包导致条目被删除。

但是你可以切换到POSIXct表示来避免这个缺点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

于 2012-09-04T11:45:57.877 回答