[R] Problem with zoo and rbind() converting matrix to vector

David Winsemius dwinsemius at comcast.net
Sat Mar 21 20:15:07 CET 2009


On Mar 21, 2009, at 2:31 PM, Ken-JP wrote:
>
> 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???

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




More information about the R-help mailing list