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
We can merge two data frames in R by using the merge() function or by using family of join() function in dplyr package. The data frames must have same column names on which the merging happens. Merge() Function in R is similar to database join operation in SQL.
To join by different variables on x and y , use a named vector. For example, by = c("a" = "b") will match x$a to y$b . To join by multiple variables, use a vector with length > 1. For example, by = c("a", "b") will match x$a to y$a and x$b to y$b .
Mutating joins combine variables from the two data sources. The next two join functions (i.e. semi_join and anti_join) are so called filtering joins. Filtering joins keep cases from the left data table (i.e. the X-data) and use the right data (i.e. the Y-data) as filter.
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"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With