zhenjiang xu
2011-Apr-15 20:31 UTC
[R] how to add two data.frame with the same column but different row numbers
Hi all, Suppose I have 2 data.frame , a and b, how can I add them together to get c? Thanks> aA a 1 b 2 c 3> bA a 6 c 1> cA a 7 b 2 c 4 -- Best, Zhenjiang [[alternative HTML version deleted]]
Dennis Murphy
2011-Apr-15 21:10 UTC
[R] how to add two data.frame with the same column but different row numbers
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) > dfmx 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. >