0

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

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

'$class' 属性似乎不同,但我看不出如何或为什么。我是否制作了这些 xts 对象,然后 all.equal 可以按预期工作。

IE .....

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 个组件的比较 >>”

4

2 回答 2

4

R 不会一致地对待 POSIXct 对象的 tzone 属性。动物园与此无关。即使根本不使用 zoo,您也可以提出这样的示例。原始代码中的 c 和 d 实际上是相等的,除了 tzone 属性。

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

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.

于 2010-03-30T10:25:07.540 回答
1

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

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说明案例。

于 2009-12-14T04:12:14.350 回答