1

This is a replication of a previous post (How to join (merge) data frames (inner, outer, left, right)?), using data.table() rather than data.frame().

Suppose I have two data tables:

library(data.table)
dt1 = data.table(CustomerID=c(1:6),Product=c(rep("Toaster",3),rep("Radio",3)))
dt2 = data.table(CustomerID=c(2,4,6),State=c(rep("Alabama",2),rep("Ohio",1)))            

dt1
   CustomerID Product
1:          1 Toaster
2:          2 Toaster
3:          3 Toaster
4:          4   Radio
5:          5   Radio
6:          6   Radio
dt2
   CustomerID   State
1:          2 Alabama
2:          4 Alabama
3:          6    Ohio

setkey(dt1,CustomerID)
setkey(dt2, CustomerID)            

The default using merge() on a data.table is a right join. What's the syntax for the others?

#Outer join:

#Right outer (data.table default): 
dtro <- merge(dt1,dt2); dtro 

   CustomerID Product   State
1:          2 Toaster Alabama
2:          4   Radio Alabama
3:          6   Radio    Ohio

#Left outer:

#Cross join:  
Community
  • 1
  • 1
kpeyton
  • 149
  • 7
  • I like the way this question moreso than the one @mnel linked to, but I agree it is a duplicate – Ricardo Saporta Apr 08 '13 at 01:58
  • 1
    `merge.data.table` takes the same `all.x` and `all.y` `all` arguments as `merge.data.frame`, so the accepted answer in the data.frame question will be correct here too. – mnel Apr 08 '13 at 02:01

0 Answers0