0

I have the following two data tables:

df1 = data.table(nr=1:10, replicate(10,sample(0:1,10,rep=TRUE)))
df2 = data.table(nr=c(1:3,11:13), replicate(4,sample(0:1,6,rep=TRUE)),V13=sample(0:1,6,rep=TRUE),V14=sample(0:1,6,rep=TRUE))

df1:

nr V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
 1  0  1  0  0  0  1  1  1  0   0
 2  0  0  1  0  0  0  0  0  0   1
 3  0  0  0  1  1  1  0  0  0   1
 4  1  0  0  0  1  1  1  0  0   1
 5  0  1  0  1  0  0  1  1  1   1
 6  1  1  1  1  0  1  1  0  1   1
 7  1  0  1  0  1  1  0  1  0   0
 8  0  1  1  1  1  0  0  0  0   0
 9  0  0  0  1  1  1  0  0  1   0
10  1  0  1  1  0  1  1  1  0   0

df2:

nr V1 V2 V3 V4 V13 V14
 1  0  1  1  1   0   0
 2  1  1  0  1   0   0
 3  0  0  1  0   0   0
11  1  0  0  1   1   1
12  0  0  0  1   0   1
13  0  0  0  1   0   1

output:

nr V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V13 V14 
 1  0  1  1  1  0  1  1  1  0   0   0   0
 2  1  1  1  1  0  0  0  0  0   1   0   0   
 3  0  0  1  1  1  1  0  0  0   1   0   0
 4  1  0  0  0  1  1  1  0  0   1   0   0
 5  0  1  0  1  0  0  1  1  1   1   0   0 
 6  1  1  1  1  0  1  1  0  1   1   0   0
 7  1  0  1  0  1  1  0  1  0   0   0   0
 8  0  1  1  1  1  0  0  0  0   0   0   0
 9  0  0  0  1  1  1  0  0  1   0   0   0
10  1  0  1  1  0  1  1  1  0   0   0   0
11  1  0  0  1  0  0  0  0  0   0   1   1
12  0  0  0  1  0  0  0  0  0   0   0   1
13  0  0  0  1  0  0  0  0  0   0   0   1

Does anyone know of a practical way to merge (outer join) these data tables and possibly add the values together if they're present in both. A simple merge doesn't seem to be appropriate for this case. So basically I would need to add a new row for every row that is not present in df1 but is in df2, add a new column for every column that is not present in df1 but is in df2 and merge (or add together) the values for those that are present in both df1 and df2.

paul_dg
  • 433
  • 5
  • 15
  • did you search? http://stackoverflow.com/questions/12773822/why-does-xy-join-of-data-tables-not-allow-a-full-outer-join-or-a-left-join – rawr Sep 08 '14 at 12:50
  • Not well enough apparently, thank you! – paul_dg Sep 08 '14 at 13:00
  • @rawr This still seems different since an outer join would still not merge columns with the same names. You still either join on rows (or keys in this case) or on columns but not both, right? – paul_dg Sep 08 '14 at 13:15
  • you seem to be replacing values since, for example, there is already data in V1-V4 in rows 1-3, youre not adding them. r cant guess how you want to handle this – rawr Sep 09 '14 at 12:57

0 Answers0