My problem is that I would like to do a left join with dplyr like this:
x <- left.join(TableA, TableB)
How can I do to bring just a specific field from TableB? Imagine TableB has 3 fields x,y,z and I just want x in TableA
My problem is that I would like to do a left join with dplyr like this:
x <- left.join(TableA, TableB)
How can I do to bring just a specific field from TableB? Imagine TableB has 3 fields x,y,z and I just want x in TableA
To join both tables as desired, you have to select field x
and an id-field from TableB
for the join. You can do this with the select()
function.
Let's assume for the join that your id-field in TableB
is y
.
x <- TableA %>%
left_join(select(TableB, x, y), by = c("id" = "y"))
You subset TableB first. So you do something like:
TableB <- TableB[,'x,']
left_join(TableA,TableB)
The obvious disadvantage of this method is that we are bound to join with column x. Suppose you want to join with column y but want only x in the end result, then you can do the following:
select(left_join(TableA,TableB, by = 'y'), -c(y,z))
x <- TableA %>% left_join(select(TableB, id), by.x = "id")