[R] how to add two data.frame with the same column but different row numbers
Gabor Grothendieck
ggrothendieck at gmail.com
Sat Apr 16 01:10:52 CEST 2011
On Fri, Apr 15, 2011 at 6:10 PM, zhenjiang xu <zhenjiang.xu at gmail.com> wrote:
> Thanks, Dennis! I'll go with it. It's surprising there is no ready way to do
> that. I imagine it should be a common data manipulation to add two
> data.frame from two different sources. It could happen that one data.frame
> is missing some rows while the other have some more.
>
If you represent them as zoo series then you can do it using +
(although the definition of + is different than in your post). Here
"a", "b" and "c" are the "times":
library(zoo)
a <- zoo(1:3, letters[1:3])
b <- zoo(c(6, 1), c("a", "c"))
a+b
The last line gives:
> a+b
a c
7 4
To use the definition in your post one could do this (which has the
effect of modifying b so that a+b works as in your post):
merge(a, b, fill = 0, retclass = NULL)
a+b
The last line gives:
> a+b
a b c
7 2 4
--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com
More information about the R-help
mailing list