Bird
Raised Fist0
R Programmingprogramming~20 mins

join functions (left_join, inner_join) in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Join Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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)
A
  id value1 value2
1  2      B      X
2  3      C      Y
B
  id value1 value2
1  1      A   &lt;NA&gt;
2  2      B      X
3  3      C      Y
4  4   &lt;NA&gt;      Z
C
  id value1 value2
1  2      B      Y
2  3      C      Z
3  4   &lt;NA&gt;      Z
D
  id value1 value2
1  1      A      X
2  2      B      Y
3  3      C      Z
Attempts:
2 left
💡 Hint
inner_join keeps only rows with matching keys in both data frames.
❓ Predict Output
intermediate
2: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)
A
  id value1 value2
1  2      B      X
2  3      C      Y
3  4   &lt;NA&gt;      Z
B
  id value1 value2
1  1      A      X
2  2      B      Y
3  3      C      Z
C
  id value1 value2
1  1      A      Z
2  2      B      X
3  3      C      Y
4  4   &lt;NA&gt;      Z
D
  id value1 value2
1  1      A   &lt;NA&gt;
2  2      B      X
3  3      C      Y
Attempts:
2 left
💡 Hint
left_join keeps all rows from the first data frame and adds matching rows from the second.
🔧 Debug
advanced
2: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)
AError: by variables must have same names in both data frames
BError: val and val2 columns have different types
CError: object 'id' not found in df1
DNo error, prints joined data frame
Attempts:
2 left
💡 Hint
Check the column names used in the join condition.
🧠 Conceptual
advanced
1:30remaining
Difference between left_join and inner_join
Which statement correctly describes the difference between left_join and inner_join in R's dplyr?
Aleft_join returns only matching rows from both tables; inner_join returns all rows from the left table
Bleft_join returns all rows from the left table and matching rows from the right; inner_join returns only matching rows from both tables
Cleft_join returns all rows from the right table; inner_join returns all rows from the left table
Dleft_join and inner_join return the same result but in different order
Attempts:
2 left
💡 Hint
Think about which rows are kept from each table.
❓ Predict Output
expert
2: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)
A
  id val1 val2 val3
1  3    C    Y    M
2  4 &lt;NA&gt;    Z    N
B
  id val1 val2 val3
1  2    B    X    M
2  3    C    Y    N
C
  id val1 val2 val3
1  3    C    Y    M
D
  id val1 val2 val3
1  1    A &lt;NA&gt; &lt;NA&gt;
2  2    B    X &lt;NA&gt;
3  3    C    Y    M
Attempts:
2 left
💡 Hint
Remember left_join keeps all df1 rows, then inner_join keeps only rows matching df3.

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

  1. Step 1: Understand the purpose of left_join

    left_join keeps all rows from the first (left) table regardless of matches.
  2. 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.
  3. Final Answer:

    Keeps all rows from the first table and adds matching rows from the second table. -> Option D
  4. 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

  1. Step 1: Identify correct function name and parameters

    The function is inner_join from dplyr, and it uses by to specify join columns.
  2. 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.
  3. Final Answer:

    inner_join(df1, df2, by = "id") -> Option C
  4. Quick Check:

    inner_join syntax uses by = "column" [OK]
Hint: Use inner_join() with by = "column" for correct syntax [OK]
Common Mistakes:
  • Using 'on' instead of 'by' for join column
  • Misspelling function name as innerjoin
  • Confusing parameter order
3. Given these two data frames:
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"))

What is the result of inner_join(df1, df2, by = "id")?
medium
A. Rows with ids 2 and 3 with columns from both data frames
B. Rows with ids 1, 2, 3, 4 with all columns
C. Rows with ids 1 and 4 only
D. Rows with ids 1, 2, 3 with only columns from df1

Solution

  1. Step 1: Identify matching ids in both data frames

    df1 has ids 1, 2, 3; df2 has ids 2, 3, 4. Common ids are 2 and 3.
  2. Step 2: Understand inner_join output

    inner_join keeps only rows with matching ids and combines columns from both data frames for those rows.
  3. Final Answer:

    Rows with ids 2 and 3 with columns from both data frames -> Option A
  4. Quick Check:

    inner_join = only matching rows [OK]
Hint: inner_join keeps only rows with matching keys [OK]
Common Mistakes:
  • Including unmatched rows in inner_join result
  • Confusing inner_join with left_join
  • Ignoring columns from second data frame
4. You run this code:
left_join(df1, df2, by = "id")

but get an error: Error: object 'df2' not found. What is the likely cause?
medium
A. The data frame df2 was not created or loaded before the join.
B. The by argument is misspelled.
C. left_join cannot join on column named 'id'.
D. You need to use inner_join instead of left_join.

Solution

  1. Step 1: Analyze the error message

    The error says df2 is not found, meaning R cannot find the object named df2.
  2. Step 2: Identify cause of missing object

    This usually means df2 was not created, loaded, or is misspelled before calling left_join.
  3. Final Answer:

    The data frame df2 was not created or loaded before the join. -> Option A
  4. Quick Check:

    Missing data frame causes object not found error [OK]
Hint: Check if all data frames exist before joining [OK]
Common Mistakes:
  • Assuming join syntax error causes object not found
  • Ignoring typo in data frame names
  • Thinking join functions require special packages loaded
5. You have two data frames:
students <- data.frame(student_id = c(1, 2, 3, 4), name = c("Anna", "Ben", "Cara", "Dan"))
scores <- data.frame(student_id = c(2, 3, 5), score = c(88, 92, 75))

You want a table that shows all students with their scores if available, and NA if no score exists. Which join function and code should you use?
hard
A. inner_join(students, scores, by = "student_id")
B. left_join(students, scores, by = "student_id")
C. right_join(students, scores, by = "student_id")
D. full_join(students, scores, by = "student_id")

Solution

  1. Step 1: Understand the goal

    You want all students listed, even if they don't have a score, with scores added where available.
  2. Step 2: Choose the correct join

    left_join keeps all rows from the first table (students) and adds matching scores, filling with NA if no match.
  3. Final Answer:

    left_join(students, scores, by = "student_id") -> Option B
  4. Quick Check:

    left_join keeps all left rows, adds matches, fills NA [OK]
Hint: Use left_join to keep all from first table with matches [OK]
Common Mistakes:
  • Using inner_join which drops students without scores
  • Using right_join which keeps all scores but drops some students
  • Using full_join which adds extra rows not needed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard