[R] Strange zoo behaviour, possible bug?

Giuseppe Milicia Giuseppe.Milicia at makoglobal.com
Wed Apr 28 17:46:23 CEST 2010

Hi there,

thanks for the reply. I agree that it's essentially a user error.

I was really wondering whether the behaviour has changed from past version of zoo and whether it'd be worth to handle this case in a ad-hoc manner. I'm using old code and I was a bit surprised I didn't bump on this earlier...

I have a function which can take as input zoo object whose coredata is either a matrix or a vector, to do that I'll have to handle the two cases slightly different. Not a bit deal, but kind of inelegant.



From: Achim Zeileis [Achim.Zeileis at uibk.ac.at]
Sent: 28 April 2010 16:25
To: Giuseppe Milicia
Cc: r-help at r-project.org
Subject: Re: [R] Strange zoo behaviour, possible bug?

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.


---- MAKO ----
This email and any files transmitted with it are confide...{{dropped:14}}

More information about the R-help mailing list