Tolga Uzuner
2008-Nov-16 12:59 UTC
[R] inconsistency between timeSeries and zoo causing a problem with rbind
Dear R Users and maintainers of packages zoo and timeSeries,
I believe there is a recently introduced inconsistency between
timeSeries and zoo which is causing a problem with rbind. I had
previously reported that I was having problems with rbind in the
following code:
library(zoo)
foo<-zoo(1,order.by=as.Date("2007-10-09"))
bar<-zoo(2,order.by=as.Date("2007-10-10"))
bar <- rbind(zoo(0, order.by = index(foo)[1]), bar)
bar
Gabor Grothendieck and Dieter Menne tried this code and wrote back
saying it was working fine. I have also tried this in a fresh session
and it works fine if the only package I load is zoo. It appears that the
code fails if one simultaneously loads packages zoo and timeSeries
library(zoo)
library(timeSeries)
foo<-zoo(1,order.by=as.Date("2007-10-09"))
bar<-zoo(2,order.by=as.Date("2007-10-10"))
bar <- rbind(zoo(0, order.by = index(foo)[1]), bar)
bar
sessionInfo()
This leads to the error I had previously reported, please see below. I
am R 2.8.0, zoo 1.5-4 and timeSeries 280.78. I believe this is
potentially a bug: I was not getting this issue even though I have been
using zoo and timeSeries in the same session since the most recent
updates to these packages.
Is this a bug ? If so, can it be corrected somehow ?
Thanks in advance,
Tolga
> library(zoo)
Attaching package: 'zoo'
The following object(s) are masked from package:base :
as.Date.numeric
> library(timeSeries)
Loading required package: timeDate
Attaching package: 'timeSeries'
The following object(s) are masked from package:zoo :
time<-
> foo<-zoo(1,order.by=as.Date("2007-10-09"))
> bar<-zoo(2,order.by=as.Date("2007-10-10"))
> bar <- rbind(zoo(0, order.by = index(foo)[1]), bar)
Error in rep.int("", nrow(r)) : incorrect type for second argument
<<<<<<<<< ERROR
> bar
2007-10-10
2
> sessionInfo()
R version 2.8.0 (2008-10-20)
i386-pc-mingw32
locale:
LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United
Kingdom.1252;LC_MONETARY=English_United
Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] timeSeries_280.78 timeDate_280.80 zoo_1.5-4
loaded via a namespace (and not attached):
[1] grid_2.8.0 lattice_0.17-15
>
Achim Zeileis
2008-Nov-16 16:03 UTC
[R] inconsistency between timeSeries and zoo causing a problem with rbind
On Sun, 16 Nov 2008, Tolga Uzuner wrote:> I believe there is a recently introduced inconsistency between timeSeries and > zoo which is causing a problem with rbind. I had previously reported that I > was having problems with rbind in the following code: > > library(zoo) > foo<-zoo(1,order.by=as.Date("2007-10-09")) > bar<-zoo(2,order.by=as.Date("2007-10-10")) > bar <- rbind(zoo(0, order.by = index(foo)[1]), bar) > barWhat happens is the following: - There is base:::rbind() and methods:::rbind(). The former is just an .Internal() call while the latter additionally does consistency checks. The consistency checks require that you can only rbind() if the result has a dimension. - base:::rbind() is usually found first, even if "methods" is loaded. But if you load "timeSeries" this changes and methods:::rbind() is found first. I haven't figured out why this happens but it does _not_ for other S4 packages (such as "Matrix" for example). - Thus, if you rbind 1d zoo series, the result is also 1d and has no dimension. If "timeSeries" is loaded, methods:::rbind() is called which assumes a dimension and fails if there is none. What should be done: - I'm not sure whether "timeSeries" can be changed so that base:::rbind() is still found first. - Maybe methods:::rbind() could be made more liberal so that it does not fail for 1d return values. Of course, one could argue that a row bind only makes sense if there are rows and columns. But, then again, maybe it is possible to allow other usages as well. - You can work around this as a user of zoo series, by calling c() instead of rbind() for 1d series, i.e., c(foo, bar) always works with or without "timeSeries" attached. Moreover, c() even works for 2d zoo series. hth, Z