Skip to content

how to add two data.frame with the same column but different row numbers

5 messages · zhenjiang xu, Dennis Murphy, Gabor Grothendieck

#
Hi:

Here's one approach:
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:
#
On Fri, Apr 15, 2011 at 6:10 PM, zhenjiang xu <zhenjiang.xu at gmail.com> wrote:
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 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 c
7 2 4