rbind.zoo 似乎没有创建一致的动物园对象

发布于 2024-08-15 13:11:02 字数 795 浏览 4 评论 0原文

我想将两个动物园对象 rbind.zoo 在一起。当我测试时,我遇到了以下问题(?)...

注意:下面是一个示例,除了说明之外显然没有任何意义。 我有一个动物园对象,称之为“X”。我想将其分成两部分,然后将它们 rbind.zoo 在一起。当我将它与原始对象进行比较时, all.equal 会给出差异。

看来“$class”属性不同,但我不明白如何或为什么不同。如果我制作了这些 xts 对象,那么 all.equal 就会按预期工作。

即 .....

X.date <- as.POSIXct(paste("2003-", rep(1:4, 4:1), 
                     "-", sample(1:28, 10, replace = TRUE), sep = ""))

X <- zoo(matrix(rnorm(24), ncol = 2), X.date)

a <- X[c(1:3), ]      # first 3 elements

b <- X[c(4:6), ]      # second 3 elements

c <- rbind.zoo(a, b)  # rbind into an object of 6 elements

d <- X[c(1:6), ]      # all 6 elements

all.equal(c, d)       # are they equal?

~~~~

all.equal 给了我以下区别:

“属性:<组件 3:属性:<长度不匹配:前 1 个组件的比较>>”

I want to rbind.zoo two zoo object together. When I was testing I came across the following issue(?)...

Note: The below is an example, there is clearly no point to it apart from being illustrative.
I have an zoo object, call it, 'X'. I want to break it into two parts and then rbind.zoo them together. When I compare it to the original object then all.equal gives differences.

It appears that the '$class' attribute differs, but I can't see how or why. Is I make these xts objects then the all.equal works as expected.

i.e. .....

X.date <- as.POSIXct(paste("2003-", rep(1:4, 4:1), 
                     "-", sample(1:28, 10, replace = TRUE), sep = ""))

X <- zoo(matrix(rnorm(24), ncol = 2), X.date)

a <- X[c(1:3), ]      # first 3 elements

b <- X[c(4:6), ]      # second 3 elements

c <- rbind.zoo(a, b)  # rbind into an object of 6 elements

d <- X[c(1:6), ]      # all 6 elements

all.equal(c, d)       # are they equal?

~~~~

all.equal gives me the following difference:

"Attributes: < Component 3: Attributes: < Length mismatch: comparison on first 1 components > >"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戈亓 2024-08-22 13:11:02

R 对待 POSIXct 对象的 tzone 属性的方式不一致。动物园与此无关。即使根本不使用动物园,你也可以想出这样的例子。原始代码中的 c 和 d 实际上是相等的,除了 tzone 属性之外。

如果删除 tzone 属性,则 c 和 d 将相等:

attr(X.date, "tzone") <- NULL  ##### add after defining X.date

对于 Zoo,因为它支持几乎任何时间类,如果不需要时区,您可以考虑使用不同的时间类。如果您不需要时区,那么它只会让事情变得毫无理由地复杂化。例如,在此处的示例中,我们可以使用 Date 类。

请阅读 R 新闻 4/1 了解更多信息。

R does not treat the tzone attribute of POSIXct objects consistently. zoo has nothing to do with this. You can come up with examples like this even without using zoo at all. c and d in the original code actually are equal except for the tzone attribute.

If you remove the tzone attribute then c and d will be equal:

attr(X.date, "tzone") <- NULL  ##### add after defining X.date

In the case of zoo since it supports just about any time class you could consider using a different time class if you do not need time zones. Time zones just complicate things for no good reason if you do not need them. In the example here we could have used Date class, for example.

Read R News 4/1 for more.

笨笨の傻瓜 2024-08-22 13:11:02

是的,您可能已经发现了属性处理方面的错误。另一方面,谁在乎呢? cd 实际上是相等的:

R> c - d

2003-01-07 0 0
2003-01-15 0 0
2003-01-17 0 0
2003-01-18 0 0
2003-02-17 0 0
2003-02-22 0 0
R> 

检查对象不会透露任何信息:

R> str(c)
‘zoo’ series from 2003-01-07 to 2003-02-22
  Data: num [1:6, 1:2] 0.79 -0.731 1.574 -0.694 0.358 ...
  Index:  POSIXct[1:6], format: "2003-01-07" "2003-01-15" 
      "2003-01-17" "2003-01-18" "2003-02-17" "2003-02-22"
R> str(d)
‘zoo’ series from 2003-01-07 to 2003-02-22
  Data: num [1:6, 1:2] 0.79 -0.731 1.574 -0.694 0.358 ...
  Index:  POSIXct[1:6], format: "2003-01-07" "2003-01-15" 
      "2003-01-17" "2003-01-18" "2003-02-17" "2003-02-22"

我建议您向 zoo 的维护者发送一封礼貌的邮件来说明情况。

Yes, you may have found a bug there in terms of the handling of attributes. On the other hand, who cares? c and d are effectively equal:

R> c - d

2003-01-07 0 0
2003-01-15 0 0
2003-01-17 0 0
2003-01-18 0 0
2003-02-17 0 0
2003-02-22 0 0
R> 

Inspecting the objects gives nothing away:

R> str(c)
‘zoo’ series from 2003-01-07 to 2003-02-22
  Data: num [1:6, 1:2] 0.79 -0.731 1.574 -0.694 0.358 ...
  Index:  POSIXct[1:6], format: "2003-01-07" "2003-01-15" 
      "2003-01-17" "2003-01-18" "2003-02-17" "2003-02-22"
R> str(d)
‘zoo’ series from 2003-01-07 to 2003-02-22
  Data: num [1:6, 1:2] 0.79 -0.731 1.574 -0.694 0.358 ...
  Index:  POSIXct[1:6], format: "2003-01-07" "2003-01-15" 
      "2003-01-17" "2003-01-18" "2003-02-17" "2003-02-22"

I suggest you send a polite mail to the maintainers of zoo illustrating the case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文