Joining
Data Frames in R
Learn how to combine separate data tables using left_join() and inner_join(), and understand how row counts change when you merge data.
Why Joining Data Matters
Real-world data rarely lives in a single table. Student grades might be in one file, while student demographics sit in another. Joining lets you combine these tables by matching rows on a shared column: a key.
The Data Frame Refresher
A data frame in R is a table where each column is a variable and each row is an observation. Joining operates on two data frames at a time.
The dplyr Package
R's dplyr package (part of the tidyverse) provides clean, readable join functions. Before joining, always load it:
How left_join() Works
left_join() starts with every row in the left table and tries to find a match in the right table. If no match exists, the right-side columns are filled with NA.
How inner_join() Works
inner_join() keeps only the rows where the key exists in both tables. Unmatched rows from either side are dropped entirely.
left_join vs inner_join
| left_join() | inner_join() | |
|---|---|---|
| Unmatched left rows | Kept (with NA) | Dropped |
| Result row count | \(\ge\) left table rows | \(\le\) left table rows |
| NA values possible? | Yes | No |
| Use when | You need all left data | You only want matches |
Interpreting Row Count Changes
After a join, always check how the number of rows changed. The row count tells you a story about your data.
One-to-Many Relationships
When one key value in the left table matches multiple rows in the right table, the left row is duplicated for each match. This is called a one-to-many join.
Step-by-Step Join Workflow
Example 1: Simple left_join
You have a table of products and a table of prices. Not every product has a price yet.
| item | category | price |
|---|---|---|
| apple | fruit | 1.20 |
| banana | fruit | NA |
| cherry | fruit | 3.50 |
Example 2: inner_join Filtering
Same data, but now using inner_join. Watch what happens to the banana row.
| item | category | price |
|---|---|---|
| apple | fruit | 1.20 |
| cherry | fruit | 3.50 |
Example 3: Joining with Different Key Names
Sometimes the key column has a different name in each table. Use the by = c("left_name" = "right_name") syntax.
| order_id | customer_code | name |
|---|---|---|
| 101 | A | Alice |
| 102 | B | Bob |
| 103 | A | Alice |
Using the Pipe Operator
In real R code, you will often see joins written with the pipe operator (%>% or |>), which passes the left data frame as the first argument.
Common Mistakes
Summary Cheat Sheet
| left_join() | inner_join() | |
|---|---|---|
| Syntax | left_join(x, y, by) | inner_join(x, y, by) |
| Keeps from left | All rows | Only matches |
| Keeps from right | Only matches | Only matches |
| Unmatched left rows | Filled with NA | Dropped |
| Row count change | \(\ge\) left rows | \(\le\) left rows |
| Best for | Preserving all data | Filtering to matches |
Question 1 of 5
You have a students table with 10 rows and a grades table with 8 rows. After running left_join(students, grades, by = "id"), the result has 10 rows. What does this tell you?
Question 2 of 5
What happens to unmatched rows from the left table in an inner_join()?
Question 3 of 5
You run left_join(orders, items, by = "product_id"). The orders table has 50 rows, but the result has 65 rows. What most likely happened?
Question 4 of 5
The key in table A is character type ("1") while the key in table B is numeric (1). What is the best practice to ensure a reliable join?
Question 5 of 5
Which code correctly joins two data frames where the key is called student_id in the left table and sid in the right table?