3

我有一个相当奇怪的问题,可能最好用 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 并比较 的摘要显示已在索引中创建了超过 36,000 个 NA metom

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

4

1 回答 1

4

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

zoo在从到类信息的转换中的某个地方xtschron破坏了。

将您的索引转换为类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都指向相同的帮助页面。


编辑

如果xts应该chron在从 导入时处理对象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 之间的转换中,转换代码应该针对用户的语言环境进行调整。

我建议您与包作者联系并提供此信息。

于 2011-12-09T14:21:25.800 回答