📖 Scenario: You work in a small bookstore. You have two tables: one with book details and another with sales data. You want to combine these tables to see which books sold and their details.
🎯 Goal: Learn how to use left_join and inner_join functions in R to combine two data frames based on a common column.
📋 What You'll Learn
Create two data frames named books and sales with exact data
Create a variable key_column with the column name to join on
Use left_join to join books and sales on key_column
Use inner_join to join books and sales on key_column
Print the results of both joins
💡 Why This Matters
🌍 Real World
Combining data from different sources is common in data analysis, like joining customer info with purchase records.
💼 Career
Knowing how to join tables is essential for data scientists, analysts, and anyone working with databases or data frames.
Progress0 / 4 steps
1
Create the initial data frames
Create a data frame called books with columns book_id and title containing these rows: (1, "The Alchemist"), (2, "1984"), (3, "To Kill a Mockingbird"). Also create a data frame called sales with columns book_id and copies_sold containing these rows: (1, 150), (3, 200), (4, 50).
R Programming
Hint
Use data.frame() to create each table with the exact column names and values.
2
Create the join key variable
Create a variable called key_column and set it to the string "book_id" to use as the join column.
R Programming
Hint
Assign the string "book_id" to the variable key_column.
3
Perform left_join and inner_join
Use left_join to join books and sales on key_column and save the result in left_joined. Then use inner_join to join books and sales on key_column and save the result in inner_joined.
R Programming
Hint
Use left_join(books, sales, by = key_column) and assign it to left_joined. Similarly, use inner_join(books, sales, by = key_column) and assign it to inner_joined.
4
Print the joined data frames
Print the left_joined data frame, then print the inner_joined data frame.
R Programming
Hint
Use print(left_joined) and print(inner_joined) to show the results.
Practice
(1/5)
1. What does the left_join function do in R's dplyr package?
easy
A. Removes all rows that have any missing values.
B. Keeps only rows that match in both tables.
C. Keeps all rows from the second table and adds matching rows from the first table.
D. Keeps all rows from the first table and adds matching rows from the second table.
Solution
Step 1: Understand the purpose of left_join
left_join keeps all rows from the first (left) table regardless of matches.
Step 2: Understand how matching rows are added
It adds columns from the second (right) table only where keys match, filling with NA if no match.
Final Answer:
Keeps all rows from the first table and adds matching rows from the second table. -> Option D
Quick Check:
left_join = all left rows + matching right rows [OK]
Hint: Remember: left_join keeps all from left, adds matches [OK]
Common Mistakes:
Confusing left_join with inner_join
Thinking left_join removes unmatched rows
Mixing up which table is 'left' or 'right'
2. Which of the following is the correct syntax to perform an inner join of two data frames df1 and df2 by the column id in R?
easy
A. inner_join(df1, df2, on = "id")
B. innerjoin(df1, df2, on = "id")
C. inner_join(df1, df2, by = "id")
D. innerjoin(df1, df2, by = "id")
Solution
Step 1: Identify correct function name and parameters
The function is inner_join from dplyr, and it uses by to specify join columns.
Step 2: Check parameter names and spelling
Correct syntax is inner_join(df1, df2, by = "id"). Other options use wrong function names or parameter names.
Final Answer:
inner_join(df1, df2, by = "id") -> Option C
Quick Check:
inner_join syntax uses by = "column" [OK]
Hint: Use inner_join() with by = "column" for correct syntax [OK]