Given various one-row xts objects:
z1 = xts(t(c("9902"=0,"9903"=0,"9904"=0,"9905"=2,"9906"=2)),as.Date("2015-01-01"))
z2 = xts(t(c("9902"=3,"9903"=4,"9905"=6,"9906"=5,"9908"=8)),as.Date("2015-01-02"))
z3 = xts(t(c("9901"=1,"9903"=3,"9905"=5,"9906"=6,"9907"=7,"9909"=9)),as.Date("2015-01-03"))
I want to merge them into a single xts object. But cbind(z1,z2,z3)
gives:
X9902 X9903 X9904 X9905 X9906 X9902.1 X9903.1 X9905.1 X9906.1 X9908 X9901 X9903.2 X9905.2 X9906.2 X9907 X9909
2015-01-01 0 0 0 2 2 NA NA NA NA NA NA NA NA NA NA NA
2015-01-02 NA NA NA NA NA 3 4 6 5 8 NA NA NA NA NA NA
2015-01-03 NA NA NA NA NA NA NA NA NA NA 1 3 5 6 7 9
Whereas what I expect is:
9901 9902 9903 9904 9905 9906 9907 9908 9909
2015-01-01 0 0 0 0 2 2 0 0 0
2015-01-02 0 3 4 0 6 5 0 8 0
2015-01-03 1 0 3 0 5 6 7 0 9
(I can get the NAs changed to zeroes with giving fill=0
, i.e. cbind(z1,z2,z3,fill=0)
.)
rbind(z1,z2,z3)
complains that the rows have different number of columns. But, I believe if if the missing columns were added to each xts object in advance that this would be a good approach?
The real data may have 1000s of rows, and a few hundred columns (once merged), so I've got one eye on efficiency.