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

From: Dennis Murphy <djmuser_at_gmail.com>
Date: Fri, 15 Apr 2011 14:10:18 -0700

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.
>



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 - 21:39:36 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 Fri 15 Apr 2011 - 22:20:31 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