[R] Problem with zoo and rbind() converting matrix to vector
Ken-JP
kfmfe04 at gmail.com
Sun Mar 22 01:18:29 CET 2009
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:
> x <- m[1,,drop=FALSE]
> x
inp
2003-02-01 5
> is.matrix( x )
[1] TRUE
------------------------------------
and another zoo-matrix:
> y <- lag(m,-1)
> y
inp
2003-02-02 5
2003-02-03 9
2003-02-04 4
2003-02-05 2
> is.matrix( y )
[1] TRUE
------------------------------------
and somehow, when I rbind() them together, I get a zoo-vector:
> z <- rbind( x, y )
> z
2003-02-01 2003-02-02 2003-02-03 2003-02-04 2003-02-05
5 5 9 4 2
> is.matrix( z )
[1] FALSE
> is.zoo( z )
[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:
> is.matrix( as.zoo(rbind(m[1,,drop=FALSE], lag(m,-1) )) )
[1] FALSE
> is.matrix( c(m[1,,drop=FALSE], lag(m,-1) ) )
[1] FALSE
> is.matrix( merge(c(m[1,,drop=FALSE], lag(m,-1) )) )
[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.
--
View this message in context: http://www.nabble.com/Problem-with-zoo-and-rbind%28%29-converting-matrix-to-vector-tp22638959p22641961.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list