[R] cbind giving NA's?

Petr PIKAL petr.pikal at precheza.cz
Fri Aug 26 14:08:02 CEST 2011


I was rather too quick

It has probably something to do with versions of zoo and xts

after updating to zoo 1.7.4 and xts 0.8.2 I got with your examples

> merge(a,b)
           ZWD.UGX SCHB.Close
2010-04-01      NA      28.02
2010-04-01  7.6343         NA
2010-04-02  7.6343         NA
2010-04-03  7.5458         NA
2010-04-04  7.4532         NA
2010-04-05      NA      28.30
2010-04-05  7.4040         NA
2010-04-06      NA      28.38
2010-04-06  7.3317         NA
2010-04-07      NA      28.21
2010-04-08      NA      28.31
2010-04-09      NA      28.47

but when I did

> a<-as.zoo(a)
> b<-as.zoo(b)

I got

> merge(a,b)
           ZWD.UGX SCHB.Close
2010-04-01  7.6343      28.02
2010-04-02  7.6343         NA
2010-04-03  7.5458         NA
2010-04-04  7.4532         NA
2010-04-05  7.4040      28.30
2010-04-06  7.3317      28.38
2010-04-07      NA      28.21
2010-04-08      NA      28.31
2010-04-09      NA      28.47

which is probably what you want.

Regards
Petr

> 

> Hi
> 
> > 
> > On 26 August 2011 03:37, R. Michael Weylandt 
<michael.weylandt at gmail.com> wrote:
> > > If you could, dput() them so we can see everything about them. You 
also
> > > might see if merge() gives you more expected behavior....
> > 
> > Ok...
> > > dput(a)
> > structure(c(7.6343, 7.6343, 7.5458, 7.4532, 7.404, 7.3317), class = 
c("xts",
> > "zoo"), .indexCLASS = "Date", .indexTZ = "", index = 
structure(c(1270105200,
> > 1270191600, 1270278000, 1270364400, 1270450800, 1270537200), tzone =
> > "", tclass = "Date"), .Dim = c(6L,
> > 1L), .Dimnames = list(NULL, "ZWD.UGX"))
> > > dput(b)
> > structure(c(28.02, 28.3, 28.38, 28.21, 28.31, 28.47), .indexCLASS =
> > "Date", .indexTZ = "", src = "yahoo", updated =
> > structure(1314356091.21457, class = c("POSIXct",
> > "POSIXt")), class = c("xts", "zoo"), index = structure(c(1270072800,
> > 1270418400, 1270504800, 1270591200, 1270677600, 1270764000), tzone =
> > "", tclass = "Date"), .Dim = c(6L,
> > 1L), .Dimnames = list(NULL, "SCHB.Close"))
> > > merge(a,b)
> > > merge(a,b)
> >            ZWD.UGX SCHB.Close
> > 2010-04-01      NA      28.02
> > 2010-04-01  7.6343         NA
> > 2010-04-02  7.6343         NA
> > 2010-04-03  7.5458         NA
> > 2010-04-04  7.4532         NA
> > 2010-04-05      NA      28.30
> > 2010-04-05  7.4040         NA
> > 2010-04-06      NA      28.38
> > 2010-04-06  7.3317         NA
> > 2010-04-07      NA      28.21
> > 2010-04-08      NA      28.31
> > 2010-04-09      NA      28.47
> > > q()

> I get slightly different result
> 
> > xx<-(merge(a,b))
> > xx
>            ZWD.UGX SCHB.Close
> 1270072800      NA      28.02
> 1270105200  7.6343         NA
> 1270191600  7.6343         NA
> 1270278000  7.5458         NA
> 1270364400  7.4532         NA
> 1270418400      NA      28.30
> 1270450800  7.4040         NA
> 1270504800      NA      28.38
> 1270537200  7.3317         NA
> 1270591200      NA      28.21
> 1270677600      NA      28.31
> 1270764000      NA      28.47
> 
> but from what you want it seems to me that aggregate could be suitable 
second step.
> 
> something like
> 
> aggregate(xx, list(those dates you have but I don't), mean, na.rm=T)
> 
> could do what you want.
> 
> Regards
> Petr
> 
> > 
> > So, no, merge doesn't work...
> > -- 
> > Sent from my mobile device
> > Envoyait de mon telephone mobil
> > 
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list