rjoinmergedata.table

Left join using data.table


Suppose I have two data.table's:

dataA:

   A  B
1: 1 12
2: 2 13
3: 3 14
4: 4 15

dataB:

   A  B
1: 2 13
2: 3 14

and I have the following code:

merge_test = merge(dataA, dataB, by="A", all.data=TRUE)

I get:

   A B.x B.y
1: 2  13  13
2: 3  14  14

However, I want all the rows in dataA in the final merged table. Is there a way to do this?


Solution

  • You can try this:

    # used data
    # set the key in 'B' to the column which you use to join
    A <- data.table(a = 1:4, b = 12:15)
    B <- data.table(a = 2:3, b = 13:14, key = 'a') 
    
    B[A]