Bird
Raised Fist0
R Programmingprogramming~20 mins

select() for column selection in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
select() Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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)
AA tibble with columns a, b, c, d
BA tibble with columns a and c
CA tibble with columns b and d
DAn error because ends_with() is used incorrectly
Attempts:
2 left
💡 Hint
Think about which columns start with 'b' and which end with 'd'.
❓ Predict Output
intermediate
2: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)
AColumns x1, x2, y1, y2
BColumns x1, x2, y1
CColumns x1 and y1 only
DError because x1:y1 is invalid
Attempts:
2 left
💡 Hint
The colon operator selects columns between two names in order.
🔧 Debug
advanced
2: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)
AColumn 'c' does not exist in the data frame
Bselect() cannot take multiple arguments
CThe pipe operator %>% is used incorrectly
DThe tibble function is missing parentheses
Attempts:
2 left
💡 Hint
Check if all columns you select exist in the data frame.
❓ Predict Output
advanced
2: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)
AA tibble with columns 'age' and 'score' only
BA tibble with columns 'name' and 'age' unchanged
CAn error because select() cannot rename columns
DA tibble with columns 'person' and 'age', where 'person' has the names
Attempts:
2 left
💡 Hint
select() can rename columns by using new_name = old_name syntax.
🧠 Conceptual
expert
3: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)
AOnly column 'a1' is selected because 'a2' is excluded
BAn error occurs because negative selection cannot be combined with matches()
CColumns 'a1', 'a2', and 'b1' are selected
DColumns 'a1' and 'a2' are selected because matches() includes both
Attempts:
2 left
💡 Hint
Negative selection removes columns after positive selection.

Practice

(1/5)
1. What does the select() function do in R's dplyr package?
easy
A. It adds new columns to the data frame.
B. It filters rows based on conditions.
C. It chooses specific columns from a data frame.
D. It sorts the data frame by a column.

Solution

  1. Step 1: Understand the purpose of select()

    The select() function is designed to pick columns from a data frame, not rows or sorting.
  2. Step 2: Compare with other functions

    Functions like filter() handle rows, arrange() sorts, and mutate() adds columns, so these are not what select() does.
  3. Final Answer:

    It chooses specific columns from a data frame. -> Option C
  4. Quick Check:

    select() = pick columns [OK]
Hint: Remember: select() picks columns, filter() picks rows [OK]
Common Mistakes:
  • Confusing select() with filter()
  • Thinking select() sorts data
  • Assuming select() adds columns
2. Which of the following is the correct syntax to select columns name and age from a data frame df using dplyr?
easy
A. select(name, age)
B. df.select(name, age)
C. select(name, age, df)
D. df %>% select(name, age)

Solution

  1. Step 1: Recall dplyr pipe syntax

    In dplyr, the pipe operator %>% passes df as the first argument to select().
  2. Step 2: Check each option

    A: select(name, age) misses the data frame df. B: df %>% select(name, age) uses pipe correctly. C: select(name, age, df) has wrong argument order. D: df.select(name, age) is not valid R syntax.
  3. Final Answer:

    df %>% select(name, age) -> Option D
  4. Quick Check:

    Use pipe %>% then select columns [OK]
Hint: Use pipe %>% before select() for clean code [OK]
Common Mistakes:
  • Wrong argument order in select()
  • Using dot notation like df.select()
  • Forgetting to load dplyr package
3. Given the data frame df with columns id, name, age, and score, what will be the output of this code?
library(dplyr)
df %>% select(name, score)
medium
A. A data frame with only columns name and score.
B. A data frame with all columns except name and score.
C. An error because select() needs row filtering.
D. A data frame with columns id and age only.

Solution

  1. Step 1: Understand select() behavior

    The select() function picks only the columns named inside it, here name and score.
  2. Step 2: Confirm output columns

    The output will be a data frame with just these two columns, dropping id and age.
  3. Final Answer:

    A data frame with only columns name and score. -> Option A
  4. Quick Check:

    select(name, score) = only those columns [OK]
Hint: select() keeps only listed columns, drops others [OK]
Common Mistakes:
  • Thinking select() drops listed columns
  • Expecting row filtering from select()
  • Confusing select() with filter()
4. Identify the error in this code snippet:
library(dplyr)
df %>% select('name', 'age')
medium
A. Missing pipe operator before select().
B. Using quotes around column names causes an error.
C. select() cannot be used with dplyr.
D. The data frame df is not defined.

Solution

  1. Step 1: Check column name syntax in select()

    In dplyr's select(), column names should be unquoted bare names, not strings.
  2. Step 2: Understand consequences of quotes

    Using quotes treats them as strings, causing select() to fail to find columns and throw an error.
  3. Final Answer:

    Using quotes around column names causes an error. -> Option B
  4. Quick Check:

    Column names unquoted in select() [OK]
Hint: Do not put quotes around column names in select() [OK]
Common Mistakes:
  • Quoting column names inside select()
  • Forgetting to load dplyr
  • Assuming select() filters rows
5. You have a data frame df with columns name, age, score, and grade. You want to select only name and score but rename score to points in the output. Which code does this correctly?
hard
A. df %>% select(name, points = score)
B. df %>% select(name, score = points)
C. df %>% rename(points = score) %>% select(name, score)
D. df %>% select(name, score) %>% mutate(points = score)

Solution

  1. Step 1: Understand renaming in select()

    dplyr allows renaming columns inside select() by using new_name = old_name syntax.
  2. Step 2: Check each option

    A: df %>% rename(points = score) %>% select(name, score) renames score to points, then selects score which no longer exists, error. B: df %>% select(name, score = points) reverses names, no points column exists, error. C: df %>% select(name, points = score) correctly selects name and renames score to points. D: df %>% select(name, score) %>% mutate(points = score) adds points but keeps score.
  3. Final Answer:

    df %>% select(name, points = score) -> Option A
  4. Quick Check:

    Rename inside select() with new = old [OK]
Hint: Rename columns inside select() using new_name = old_name [OK]
Common Mistakes:
  • Swapping new and old names
  • Using rename() without dropping old column
  • Using mutate() instead of select() for renaming
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