📖 Scenario: You work in a small company that keeps customer and order information in separate tables. You want to combine these tables to see which customers made which orders.
🎯 Goal: Build a simple R program that merges two data frames: one with customer details and one with order details, using a common column.
📋 What You'll Learn
Create two data frames with exact given data
Create a variable for the common column name
Use the merge() function with the common column
Print the merged data frame
💡 Why This Matters
🌍 Real World
Combining customer and order data is common in business to analyze sales and customer behavior.
💼 Career
Data analysts and data scientists often merge data from different sources to prepare for analysis and reporting.
Progress0 / 4 steps
1
Create the initial data frames
Create a data frame called customers with columns customer_id and name containing these rows: (1, "Alice"), (2, "Bob"), (3, "Charlie"). Also create a data frame called orders with columns order_id, customer_id, and product containing these rows: (101, 1, "Book"), (102, 2, "Pen"), (103, 1, "Notebook").
R Programming
Hint
Use data.frame() with vectors for each column to create the data frames.
2
Set the common column name
Create a variable called common_col and set it to the string "customer_id" to use as the key for merging.
R Programming
Hint
Just assign the string "customer_id" to the variable common_col.
3
Merge the data frames
Use the merge() function to merge customers and orders by the column stored in common_col. Save the result in a variable called merged_data.
R Programming
Hint
Use merge(customers, orders, by = common_col) to join the tables on the common column.
4
Print the merged data frame
Write a print() statement to display the merged_data data frame.
R Programming
Hint
Use print(merged_data) to show the combined table.
Practice
(1/5)
1. What does the merge() function do in R when working with data frames?
easy
A. It combines two data frames by matching rows based on common columns.
B. It deletes rows from a data frame.
C. It sorts a data frame alphabetically.
D. It converts a data frame into a list.
Solution
Step 1: Understand the purpose of merge()
The merge() function is designed to combine two data frames by matching rows based on one or more common columns.
Step 2: Eliminate incorrect options
Options A, B, and D describe different operations unrelated to merging data frames.
Final Answer:
It combines two data frames by matching rows based on common columns. -> Option A
Quick Check:
merge() combines data frames by matching columns [OK]
Hint: Remember: merge() joins data frames on matching columns [OK]
Common Mistakes:
Thinking merge() deletes rows
Confusing merge() with sorting functions
Assuming merge() converts data frames to lists
2. Which of the following is the correct syntax to merge two data frames df1 and df2 by a common column id in R?
easy
A. merge(df1, df2, on = "id")
B. merge(df1, df2, by = "id")
C. merge(df1, df2, key = "id")
D. merge(df1, df2, match = "id")
Solution
Step 1: Recall merge() argument names
The correct argument to specify the column to merge on is by, not on, key, or match.
Step 2: Check syntax correctness
merge(df1, df2, by = "id") uses by = "id", which is the correct syntax for merging by the column named "id".
You want to merge them to keep all rows from df1 and add matching val2 values. Which code correctly merges and keeps all df1 rows, including duplicates, showing NA where no match exists?
hard
A. merge(df1, df2, by = "id", all = TRUE)
B. merge(df1, df2, by = "id", all.y = TRUE)
C. merge(df1, df2, by = "id", all.x = TRUE)
D. merge(df1, df2, by = "id")
Solution
Step 1: Understand all.x = TRUE behavior
Setting all.x = TRUE keeps all rows from df1, including duplicates, and adds matching rows from df2. Non-matching rows get NA.
Step 2: Compare with other options
merge(df1, df2, by = "id", all = TRUE) keeps all rows from both data frames (full outer join), which is not required. merge(df1, df2, by = "id", all.y = TRUE) keeps all rows from df2 only. merge(df1, df2, by = "id") keeps only matching rows (inner join).
Final Answer:
merge(df1, df2, by = "id", all.x = TRUE) -> Option C
Quick Check:
all.x=TRUE keeps all df1 rows with matches or NA [OK]
Hint: Use all.x=TRUE to keep all rows from first data frame [OK]
Common Mistakes:
Using all=TRUE instead of all.x=TRUE
Using all.y=TRUE which keeps second data frame rows
Forgetting duplicates are kept automatically
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.