[R] Strange zoo behaviour, possible bug?

Achim Zeileis Achim.Zeileis at uibk.ac.at
Wed Apr 28 17:25:09 CEST 2010


On Wed, 28 Apr 2010, Giuseppe Milicia wrote:

> Hi all,
>
> I bumped into this awkward zoo behaviour. I'd be half tempted to call it 
> a bug, what do you think?

The situation could probably be improved on the zoo side but the source of 
the problem is clearly user error.

> It's annoying to work around it :(

You would just have to use it appropriately :-)
In addition to that, we can maybe improve warnings/errors.

>> t1 = zoo(-100,   as.POSIXct("2009-12-31")+(2:10)*60*60*24)
>> t2 = zoo(matrix(0), index(t1)[1]-1)

Just to be clear: t1 is a vector, t2 is a one-column matrix.

>> colnames(t1)="test"

This doesn't work and it tells you that it doesn't work.

Error in `colnames<-`(`*tmp*`, value = "test") :
   attempt to set colnames on object with less than two dimensions

If you want it to work, then the data in the time series should be a 
matrix (as you showed at the bottom of your post), e.g.:

t1 <- zoo(as.matrix(coredata(t1)), time(t1))
colnames(t1) <- "test"

If you do that, everything else will work as expected.

>> rbind(t1,t2)
>
> 2010-01-01 23:59:59    0
> 2010-01-02 00:00:00 -100
> 2010-01-03 00:00:00    0
> 2010-01-04 00:00:00 -100
> 2010-01-05 00:00:00    0
> 2010-01-06 00:00:00 -100
> 2010-01-07 00:00:00    0
> 2010-01-08 00:00:00 -100
> 2010-01-09 00:00:00    0
> 2010-01-10 00:00:00 -100
> Warning message:
> In rbind(c(-100, -100, -100, -100, -100, -100, -100, -100, -100),  :
>  number of columns of result is not a multiple of vector length (arg 1)

Yes, this is not nice, but it does warn you that something went wrong!

>> rbind(as.xts(t1),as.xts(t2))

"xts" stores everything internally as a matrix, hence there cannot be a 
confusion between vectors and matrices.

I'll have a look whether we can throw a better warning or maybe handle the 
border case of rbinding a vector with a one-column matrix.

Best,
Z



More information about the R-help mailing list