Tuesday, 29 March 2016

r - Left join using data.table



Suppose I have two data.table's:



A:



  A  B

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


B:



   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?


Answer



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]

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...