Re: [R] how to add two data.frame with the same column but different row numbers

From: zhenjiang xu <zhenjiang.xu_at_gmail.com>
Date: Fri, 15 Apr 2011 18:10:44 -0400

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.

On Fri, Apr 15, 2011 at 5:10 PM, Dennis Murphy <djmuser_at_gmail.com> wrote:

> Hi:
>
> Here's one approach:
>
> > df1 <- data.frame(x = letters[1:3], y = 1:3)
> > df2 <- data.frame(x = c('a', 'c'), z = c(6, 1))
> > dfm <- merge(df1, df2, all.x = TRUE)
> > dfm
> x y z
> 1 a 1 6
> 2 b 2 NA
> 3 c 3 1
> sumdf <- data.frame(x = dfm$x, y = rowSums(dfm[, -1], na.rm = TRUE))
> x y
> 1 a 7
> 2 b 2
> 3 c 4
>
> HTH,
> Dennis
>
> On Fri, Apr 15, 2011 at 1:31 PM, zhenjiang xu <zhenjiang.xu_at_gmail.com>
> wrote:
> > Hi all,
> >
> > Suppose I have 2 data.frame , a and b, how can I add them together to get
> c?
> > Thanks
> >> a
> > A
> > a 1
> > b 2
> > c 3
> >
> >> b
> > A
> > a 6
> > c 1
> >
> >> c
> > A
> > a 7
> > b 2
> > c 4
> >
> > --
> > Best,
> > Zhenjiang
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > 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.
> >
>

-- 
Best,
Zhenjiang

	[[alternative HTML version deleted]]

______________________________________________
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.
Received on Fri 15 Apr 2011 - 22:14:40 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 16 Apr 2011 - 02:00:30 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive