Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top