Challenge - 5 Problems
Join Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of inner_join with overlapping keys
What is the output of this R code using
inner_join from dplyr?R Programming
library(dplyr) df1 <- data.frame(id = c(1, 2, 3), value1 = c('A', 'B', 'C')) df2 <- data.frame(id = c(2, 3, 4), value2 = c('X', 'Y', 'Z')) result <- inner_join(df1, df2, by = 'id') print(result)
Attempts:
2 left
💡 Hint
inner_join keeps only rows with matching keys in both data frames.
✗ Incorrect
inner_join returns rows where the 'id' exists in both df1 and df2. Here, ids 2 and 3 are common, so only those rows appear with their respective values.
❓ Predict Output
intermediate2:00remaining
Output of left_join with non-matching keys
What will this R code print when using
left_join?R Programming
library(dplyr) df1 <- data.frame(id = c(1, 2, 3), value1 = c('A', 'B', 'C')) df2 <- data.frame(id = c(2, 3, 4), value2 = c('X', 'Y', 'Z')) result <- left_join(df1, df2, by = 'id') print(result)
Attempts:
2 left
💡 Hint
left_join keeps all rows from the first data frame and adds matching rows from the second.
✗ Incorrect
left_join keeps all rows from df1. For id=1, no match in df2, so value2 is NA. For ids 2 and 3, matching rows are joined.
🔧 Debug
advanced2:00remaining
Identify the error in join code
This R code tries to join two data frames but causes an error. What is the error?
R Programming
library(dplyr) df1 <- data.frame(id = c(1, 2), val = c('A', 'B')) df2 <- data.frame(ID = c(1, 2), val2 = c('X', 'Y')) result <- inner_join(df1, df2, by = 'id') print(result)
Attempts:
2 left
💡 Hint
Check the column names used in the join condition.
✗ Incorrect
The join uses 'by = "id"' but df2 has column 'ID' (uppercase). The names must match or be specified as a named vector.
🧠 Conceptual
advanced1:30remaining
Difference between left_join and inner_join
Which statement correctly describes the difference between
left_join and inner_join in R's dplyr?Attempts:
2 left
💡 Hint
Think about which rows are kept from each table.
✗ Incorrect
left_join keeps all rows from the first (left) table and adds matching rows from the second (right). inner_join keeps only rows with keys in both tables.
❓ Predict Output
expert2:30remaining
Output of chained joins with overlapping keys
What is the output of this R code chaining
left_join and inner_join?R Programming
library(dplyr) df1 <- data.frame(id = c(1, 2, 3), val1 = c('A', 'B', 'C')) df2 <- data.frame(id = c(2, 3, 4), val2 = c('X', 'Y', 'Z')) df3 <- data.frame(id = c(3, 4, 5), val3 = c('M', 'N', 'O')) result <- df1 %>% left_join(df2, by = 'id') %>% inner_join(df3, by = 'id') print(result)
Attempts:
2 left
💡 Hint
Remember left_join keeps all df1 rows, then inner_join keeps only rows matching df3.
✗ Incorrect
First, left_join adds val2 to df1 for matching ids (2 and 3). Then inner_join keeps only rows with ids in df3 (3,4,5). Only id=3 remains with all columns.