Skip to content
Prev 174478 / 398502 Next

Problem with zoo and rbind() converting matrix to vector

Thank you for your reply, but I am still confused.

Let me clarify...  ...my problem isn't with zoo-ness per se.
With zoo objects, there appears to be zoo-matrices and zoo-vectors.

My problem is this - I start with a zoo-matrix:
inp
2003-02-01   5
[1] TRUE

------------------------------------
and another zoo-matrix:
inp
2003-02-02   5
2003-02-03   9
2003-02-04   4
2003-02-05   2
[1] TRUE

------------------------------------
and somehow, when I rbind() them together, I get a zoo-vector:
2003-02-01 2003-02-02 2003-02-03 2003-02-04 2003-02-05 
         5          5          9          4          2
[1] FALSE
[1] TRUE

------------------------------------

Yes, I realize that x, y, and z are still zoo's (as they should be).  But
the part I can't resolve is, z has turned into a zoo-vector.  Unfortunately,
none of the 3 solutions you gave are zoo-matrices:
[1] FALSE
[1] FALSE
[1] FALSE

------------------------------------

I was using dim() as a indirect test for matrices where I could be using
is.matrix() to be more explicit; before this, I did not know if is.matrix()
would work on a zoo-matrix.  Now, I know!

It seems to me that I need to write a specialized version of my function for
vectors (whether a plain vector or a zoo vector) and another version for
matrices (whether a plain matrix or a zoo matrix).  Luckily, I am only
dealing with 2d matrices.  But I don't know if I can avoid using really ugly
class-based code testing for is.zoo(m) and is.matrix(m), etc...  ...but even
worse, my code works for a multi-column 2d zoo matrices, but not for a
single-column 2d zoo matrix.

Gabor Grothendieck mentioned in another thread that if I use S3 generics on
zoo, I can avoid using a switch on zoo and non-zoo types.  

But unfortunately, his example, like mine, only works on a multi-column
zoo-matrix, but fails on a single-column zoo-matrix (gets converted to a
zoo-vector).  So I don't have a solution yet.

I am still trying to write something which will take a:
1. vector and return a vector
2. single-column matrix and return a single-column matrix
3. multi-column matrix and return a multi-column matrix
4.-6.  zoo versions of 1.-3.

I think solutions for 1, 3, 4, and 6 have been posted, but it is not clear
to me how I should handle 2 and the zoo-version of 2 yet.