从“zoo”到“xts”的转换会在索引中创建大量 NA
我有一个相当奇怪的问题,可能最好通过 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 对象索引。但是,当我使用 as.xts
将其转换为 xts
时间序列时,一开始看起来没问题...但是 str
命令显示 NA,并将 meto
与 m
的摘要进行比较,结果表明索引中已创建了超过 36,000 个 NA!
有谁知道为什么会发生这种情况,或者我能做些什么来解决它?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您的索引属于
chron
类。我对 chron 知之甚少,但据我所知,通常更喜欢在 R 中使用 POSIX 日期时间对象,即 POSIXct 或 POSIXlt 。在从
zoo
到xts
的转换过程中,chron
类信息被破坏。将索引转换为
POSIXct
类可以解决该问题。有关使用 R 日期和时间类的更多信息,请参阅
?DateTimeClasses
、?POSIXct
或?strptime
- 所有这些都会提供相同的帮助页。编辑
如果
xts
在从zoo
导入时应该处理chron
对象,您可能发现了一个错误函数xts::xts
。问题出现在这一行:
但请注意,您的
chron
对象的格式是("d/m/y", "h:m:s")
- I从您的str(meto)
中知道这一点。仔细观察——日和月之间存在偏差。这很可能是一个区域设置问题。我相信包作者住在美国,那里的标准格式是 m/d/y,但在许多其他地方,标准格式是 d/m/y。
因此,在 Zoo 和 xts 之间的转换中,转换代码应该根据用户的区域设置进行调整。
我建议您联系软件包作者并提供此信息。
The problem is that your index is of class
chron
. I know very little aboutchron
, but AFAIK it is generally preferred to use POSIX datetime objects in R, i.e.POSIXct
orPOSIXlt
.Somewhere in the conversion from
zoo
toxts
thechron
class information gets destroyed.Converting your index to class
POSIXct
resolves the issue.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 handlechron
objects when importing fromzoo
, you have probably found a bug in the functionxts::xts
.The problem occurs in this line:
But notice that the format of your
chron
object is("d/m/y", "h:m:s")
- I know this from yourstr(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.