[R] Rbind for appending zoo objects

Gabor Grothendieck ggrothendieck at gmail.com
Tue Jan 30 11:10:05 CET 2007


That is how rbind in the core of R works too and rbind.zoo works the same.

Try:

rbind(y1, y2[, colnames(y1)])


On 1/30/07, Shubha Vishwanath Karanth <shubhak at ambaresearch.com> wrote:
> Hi R,
>
>
>
> y1 <- zoo(matrix(1:10, ncol = 2), 1:5)
>
> colnames(y1)=c("a","b")
>
> y2 <- zoo(matrix(rnorm(10), ncol = 2), 6:10)
>
> colnames(y2)=c("b","a")
>
>
>
> > y1
>
>  a  b
>
> 1 1  6
>
> 2 2  7
>
> 3 3  8
>
> 4 4  9
>
> 5 5 10
>
>
>
> > y2
>
>            b          a
>
> 6   0.9070204  0.3527630
>
> 7   1.2405943  0.8275001
>
> 8  -0.1690653 -0.1724976
>
> 9  -0.6905223 -1.1127670
>
> 10  0.3776210  0.4208908
>
>
>
> Now, I have to append these two zoo objects, y1 and y2. So, I do as
> follows:
>
>
>
> > rbind(y2,y1)
>
>            b          a
>
> 1   1.0000000  6.0000000
>
> 2   2.0000000  7.0000000
>
> 3   3.0000000  8.0000000
>
> 4   4.0000000  9.0000000
>
> 5   5.0000000 10.0000000
>
> 6   0.9070204  0.3527630
>
> 7   1.2405943  0.8275001
>
> 8  -0.1690653 -0.1724976
>
> 9  -0.6905223 -1.1127670
>
> 10  0.3776210  0.4208908
>
> >
>
>
>
> The doubts I get are as follows:
>
> 1.      The above rbind function for the zoo objects doesn't take care
> of the column names while merging. Example: Column 'a' of y1 is appended
> with column 'b' of y2. Why is this so? How do I get rid of this?
> 2.      In the rbind function, I have given y2 first and then y1. But in
> the appended data, I see the data corresponding to y1 first and then of
> y2. Is this because of ordering of the index elements of the zoo
> objects?
>
>
>
> Or, is there any other better function to append zoo objects?
>
>
>
> Thanks in advance,
>
> Shubha
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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