Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Left join with Dplyr bringing just 1 field form the other table

Tags:

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

like image 781
Suanbit Avatar asked Sep 06 '17 14:09

Suanbit


People also ask

How do I join a column from another table in R?

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.

How do I join tables in dplyr?

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 .

What is a mutating join?

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.


1 Answers

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"))
like image 60
Søren Schaffstein Avatar answered Sep 30 '22 07:09

Søren Schaffstein