I have two data.tables in R which have an identical set of columns. They contain some rows with overlapping keys, and each table contains some unique rows. I would like to sum the overlapping rows, and retain all of the unique rows from each table.
data.table.a
Key Total
1 2
3 1
4 3
5 1
data.table.b
Key Total
2 5
3 1
5 2
result
Key Total
1 2
2 5
3 2
4 3
5 3
How can I achieve this? data.table.a[data.table.b]
performs only a left join and data.table.a + data.table.b
will not work as I have differing numbers of rows.
Edit: Added reproducible code:
data.table.a <- data.table(Key=c(1,3,4,5),Total=c(2,1,3,1))
data.table.b <- data.table(Key=c(2,3,5),Total=c(5,1,2))
result <- data.table(Key=c(1,2,3,4,5),Total=c(2,5,2,3,3))