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.