2
votes

I have the following data:

a <- data.frame(ID=c("A","B","Z","H"), a=c(0,1,2,45), b=c(3,4,5,22), c=c(6,7,8,3))
> a
  ID  a  b c
1  A  0  3 6
2  B  1  4 7
3  Z  2  5 8
4  H 45 22 3

b <- data.frame(ID=c("A","B","E","W","Z","H"), a=c(9,10,11,39,5,0), b=c(4,2,7,54,12,34), c=c(12,0,34,23,13,14))

> b
   ID  a  b  c
1:  A  9  4 12
2:  B 10  2  0
3:  E 11  7 34
4:  W 39 54 23
5:  Z  5 12 13
6:  H  0 34 14

I want to merge both dataframes, keeping only rows of data.frame a and summarize the same columns, so at the end I get:

> z
  ID  a   b   c
1  A  9   7  18
2  B 11   6   7
3  Z  7  17  21
4  H 45  56  17

So far I have tried the following:

merge(a,b,by="ID",all.x=T,all.y=F)
> merge(a,b,by="ID",all.x=T,all.y=F)
  ID a.x b.x c.x a.y b.y c.y
1  A   0   3   6   9   4  12
2  B   1   4   7  10   2   0
3  H  45  22   3   0  34  14
4  Z   2   5   8   5  12  13

> join(a,b,type="left",by="ID")
  ID  a  b c  a  b  c
1  A  0  3 6  9  4 12
2  B  1  4 7 10  2  0
3  Z  2  5 8  5 12 13
4  H 45 22 3  0 34 14

I cannot manage to summarize the columns.

My dataframe is pretty big so if the solution can speed up things that would even be better.

3

3 Answers

1
votes

If your data.frame is very big, then you may consider this option:

library(data.table)

## convert data.frame to data.table
setDT(a)

## convert data.frame to data.table
setDT(b)

## merge the two data.tables
c <- merge(a,b,by='ID')

## extract names of all columns except the first one i.e. ID
col_names <- colnames(a)[-1]

## query building
col_1 <- paste0(col_names,'.x')

col_2 <- paste0(col_names,'.y')

cols <- paste(col_1,col_2,sep=',')

cols_2 <- paste0(col_names," = sum(",cols,")")

cols_3 <- paste(cols_2,collapse=',')

query <- paste0("z <- c[,.(",cols_3,"),by=ID]")

## query execution
eval(parse(text = query))
1
votes

This works at least for your example:

a <- data.frame(ID=c("A","B","Z","H"), a=c(0,1,2,45), b=c(3,4,5,22), c=c(6,7,8,3))
b <- data.frame(ID=c("A","B","E","W","Z","H"), a=c(9,10,11,39,5,0), b=c(4,2,7,54,12,34), c=c(12,0,34,23,13,14))

match_a <- na.omit(match(b$ID, a$ID))
match_b <- na.omit(match(a$ID, b$ID))

df <- cbind(ID = a$ID[match_a], a[match_a, -1] + b[match_b, -1])

First, get matching rows from a in b and vice versa, so we can be sure that we only have those rows that appear in both data frames (and we now know their row-indices in both data frames). Then, simply use vectorized additions for those matching rows, but omit ID, as factor cannot be summed up; add ID back manually.

0
votes

You cannot directly add both data frame is because both the data frames are of unequal size. To make them of equal size you can check for IDs in a which are present in b and then add them element wise.

new <- b[b$ID %in% a$ID, ]
cbind(ID = a$ID, a[-1] + new[-1])

#  ID  a  b  c
#1  A  9  7 18
#2  B 11  6  7
#3  Z  7 17 21
#4  H 45 56 17