0
0
R Programmingprogramming~20 mins

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

Choose your learning style9 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.