Learn R: Joining Tables
Learn the basics of joining tables together in R with dplyr.
StartKey Concepts
Review core concepts you need to learn to master this subject
dplyr inner_join()
dplyr full_join()
dplyr bind_rows()
dplyr join functions
Efficient Data Storage with Multiple Tables
dplyr inner_join()
dplyr inner_join()
R data frame objects can be joined together with the dplyr
function inner_join()
. Corresponding rows with a matching column value in each data frame are combined into one row of a new data frame, and non-matching rows are dropped.
dplyr
‘s inner_join()
takes two data frames as arguments and returns a new data frame with the corresponding rows merged together. Non-matching rows from each data frame are dropped in the resulting data frame.
For example, consider the sales
and targets
data frames of a t-shirt company. sales
contains the monthly revenue for the company and has two columns: month
and revenue
. targets
contains the goals for monthly revenue for each month and has two columns: month
and target
. To perform an inner join on the two data frames using dplyr
:
sales_vs_targets <- sales %>% inner_join(targets)
inner_join()
will use the month
column as the column to match on, as both the sales
and target
data frames have a month
column. The resultant data frame will only contain the matching rows from sales
and targets
.
Multiple data frames can be merged together at once by stringing multiple calls to inner_join
with the pipe %>%
.
For example, consider the same sales
and targets
data frames of a t-shirt company. An additional data frame small_medium_large
contains the number of small, medium and large t-shirts sold per month and has four columns: month
, small
, medium
, and large
. To perform an inner join on the three data frames using dplyr
:
sales_vs_targets <- sales %>% inner_join(targets) %>% inner_join(small_medium_large)
inner_join()
will use the month
column as the column to match on, as the sales
, target
, and small_medium_large
data frames have a month
column. The resultant data frame will only contain the matching rows from sales
, targets
, and small_medium_large
.
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory