从`zoo`到`xts`的转换在索引中创建了大量的NA

时间:2011-12-09 11:09:28

标签: r time-series xts zoo

我有一个相当奇怪的问题,可能最好用R会话的例子来描述。我试图使这个可重复,如下面的评论中所要求的那样。

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

示例数据集如下所示:

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

当我们转换为XTS时:

m <- as.xts(meto)

这导致以下输出:

> 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

正如您所看到的,动物园时间序列中包含大量数据,由chron对象编制索引。但是,当我使用xts将其转换为as.xts时间序列时,看起来没有问题......但是str命令显示了NA并比较了{{1}的摘要} meto表示已在索引中创建了超过36,000个NA!

有没有人知道为什么会这样,或者我能做些什么来解决它?

1 个答案:

答案 0 :(得分:4)

问题是您的索引属于chron类。我对chron知之甚少,但AFAIK通常首选使用R中的POSIX日期时间对象,即POSIXctPOSIXlt

zoo转换为xts的某处,chron类信息被破坏。

将索引转换为类POSIXct可以解决问题。

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

有关使用R日期和时间类的详细信息,请参阅?DateTimeClasses?POSIXct?strptime - 这些都会指向相同的帮助页面。


修改

如果xtschron导入时应该处理zoo个对象,您可能在函数xts::xts中发现了一个错误。

问题出现在这一行:

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

但请注意,chron对象的格式为("d/m/y", "h:m:s") - 我从str(meto)了解到这一点。仔细观察 - 日和月之间存在不一致。

这可能是一个区域问题。我相信包裹作者居住在美国,标准格式是m / d / y,但在许多其他地方标准格式是d / m / y。

所以,不知何故,在zoo和xts之间的转换中,转换代码应该根据用户的语言环境进行调整。

我建议您使用此信息与软件包作者联系。