Skip to content

Problem with zoo and rbind() converting matrix to vector

5 messages · David Winsemius, Achim Zeileis, Ken-JP

#
require( zoo )

inp <- c( 5, 9, 4, 2, 1 );
m <- zoo( cbind( inp ), as.Date("2003-02-01") + (0:(length(inp)-1)));
dim( m ) # [1] 5 1
dim( m[1,,drop=FALSE] ) # [1] 1 1 - ok
dim( lag( m, -1 )) # [1] 4 1 - ok
dim( rbind( m[1,,drop=FALSE], lag(m,-1) )) # NULL -  converted from zoo
matrix to zoo vector!?!?
# any way to keep the last line as a zoo matrix???
#
On Mar 21, 2009, at 2:31 PM, Ken-JP wrote:
Are you sure that apply dim() is a valid method of testing for zoo-ness?

 > as.zoo(rbind(m[1,,drop=FALSE], lag(m,-1) ))
2003-02-01 2003-02-02 2003-02-03 2003-02-04 2003-02-05
          5          5          9          4          2
 > dim(as.zoo(rbind(m[1,,drop=FALSE], lag(m,-1) )))
NULL
 > is.zoo(as.zoo(rbind(m[1,,drop=FALSE], lag(m,-1) )))
[1] TRUE


 > c(m[1,,drop=FALSE], lag(m,-1) )
2003-02-01 2003-02-02 2003-02-03 2003-02-04 2003-02-05
          5          5          9          4          2
 > str(c(m[1,,drop=FALSE], lag(m,-1) ))
?zoo? series from 2003-02-01 to 2003-02-05
   Data: num [1:5] 5 5 9 4 2
   Index: Class 'Date'  num [1:5] 12084 12085 12086 12087 12088
 > dim(c(m[1,,drop=FALSE], lag(m,-1) ))
NULL


 > merge(c(m[1,,drop=FALSE], lag(m,-1) ))
2003-02-01 2003-02-02 2003-02-03 2003-02-04 2003-02-05
          5          5          9          4          2
 > dim(merge(c(m[1,,drop=FALSE], lag(m,-1) )))
NULL
 > str(merge(c(m[1,,drop=FALSE], lag(m,-1) )))
?zoo? series from 2003-02-01 to 2003-02-05
   Data: num [1:5] 5 5 9 4 2
   Index: Class 'Date'  num [1:5] 12084 12085 12086 12087 12088

David Winsemius, MD
Heritage Laboratories
West Hartford, CT
#
On Sat, 21 Mar 2009, Ken-JP wrote:

            
This was a problem in the rbind() method, I've just commited a fix to the 
devel-version on R-Forge.

Thanks for the report,
Z
#
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.
#
Hi Achim,

That was a very quick reply/fix that got posted in between my reading and my
response.

I have downloaded your fix from:

svn checkout svn://r-forge.r-project.org/svnroot/zoo

for the HEAD revision and confirmed that it works as expected - I tested
using the code I posted in the first message.  I really appreciate your
response and fix.  Thank you.

Regards,
Ken