Challenge - 5 Problems
select() Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of select() with tidyselect helpers
What is the output of the following R code using
dplyr::select() with tidyselect helpers?R Programming
library(dplyr) df <- tibble(a = 1:3, b = 4:6, c = 7:9, d = 10:12) result <- df %>% select(starts_with("b"), ends_with("d")) print(result)
Attempts:
2 left
💡 Hint
Think about which columns start with 'b' and which end with 'd'.
✗ Incorrect
The select() function picks columns starting with 'b' (which is 'b') and ending with 'd' (which is 'd'). So the result has columns 'b' and 'd'.
❓ Predict Output
intermediate2:00remaining
Result of select() with numeric range
What columns are selected by this code snippet?
R Programming
library(dplyr) df <- tibble(x1 = 1:2, x2 = 3:4, y1 = 5:6, y2 = 7:8) selected <- df %>% select(x1:y1) print(selected)
Attempts:
2 left
💡 Hint
The colon operator selects columns between two names in order.
✗ Incorrect
The select(x1:y1) picks columns from x1 through y1 in the order they appear: x1, x2, y1.
🔧 Debug
advanced2:00remaining
Why does this select() call fail?
This code throws an error. What is the cause?
R Programming
library(dplyr) df <- tibble(a = 1:3, b = 4:6) selected <- df %>% select(a, c) print(selected)
Attempts:
2 left
💡 Hint
Check if all columns you select exist in the data frame.
✗ Incorrect
The error occurs because column 'c' is not present in df, so select() cannot find it.
❓ Predict Output
advanced2:00remaining
Output of select() with helper functions and renaming
What is the output of this code?
R Programming
library(dplyr) df <- tibble(name = c("Ann", "Bob"), age = c(25, 30), score = c(88, 92)) result <- df %>% select(person = name, age) print(result)
Attempts:
2 left
💡 Hint
select() can rename columns by using new_name = old_name syntax.
✗ Incorrect
The select() call renames 'name' to 'person' and keeps 'age'. So the output has columns 'person' and 'age'.
🧠 Conceptual
expert3:00remaining
Understanding select() with complex tidyselect expressions
Which option correctly describes the columns selected by this code?
R Programming
library(dplyr) df <- tibble(a1 = 1, a2 = 2, b1 = 3, b2 = 4, c1 = 5) selected <- df %>% select(matches("^a[12]$"), -a2) print(selected)
Attempts:
2 left
💡 Hint
Negative selection removes columns after positive selection.
✗ Incorrect
matches("^a[12]$") selects 'a1' and 'a2'. Then '-a2' removes 'a2', leaving only 'a1'.