Bird
Raised Fist0
R Programmingprogramming~20 mins

filter() for row selection in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Filter Mastery in R
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of filter() with multiple conditions
What is the output of the following R code using filter() from dplyr?
R Programming
library(dplyr)
df <- tibble(x = 1:5, y = c(10, 20, 30, 40, 50))
result <- filter(df, x > 2, y < 50)
print(result)
A
A tibble: 2 × 2
      x     y
  &lt;int&gt; &lt;dbl&gt;
1     3    30
2     4    40
B
A tibble: 3 × 2
      x     y
  &lt;int&gt; &lt;dbl&gt;
1     3    30
2     4    40
3     5    50
C
A tibble: 1 × 2
      x     y
  &lt;int&gt; &lt;dbl&gt;
1     4    40
D
A tibble: 4 × 2
      x     y
  &lt;int&gt; &lt;dbl&gt;
1     2    20
2     3    30
3     4    40
4     5    50
Attempts:
2 left
💡 Hint
Remember that filter() keeps rows where all conditions are TRUE.
❓ Predict Output
intermediate
2:00remaining
filter() with logical OR condition
What will be the output of this R code using filter()?
R Programming
library(dplyr)
df <- tibble(a = c('red', 'blue', 'green', 'red'), b = 1:4)
result <- filter(df, a == 'red' | b == 3)
print(result)
A
A tibble: 2 × 2
  a         b
  &lt;chr&gt; &lt;int&gt;
1 red       1
2 red       4
B
A tibble: 4 × 2
  a         b
  &lt;chr&gt; &lt;int&gt;
1 red       1
2 blue      2
3 green     3
4 red       4
C
A tibble: 3 × 2
  a         b
  &lt;chr&gt; &lt;int&gt;
1 red       1
2 blue      2
3 green     3
D
A tibble: 3 × 2
  a         b
  &lt;chr&gt; &lt;int&gt;
1 red       1
2 green     3
3 red       4
Attempts:
2 left
💡 Hint
The | operator means OR, so rows matching either condition are kept.
🔧 Debug
advanced
2:00remaining
Identify the error in filter() usage
What error will this R code produce when run?
R Programming
library(dplyr)
df <- tibble(x = 1:3, y = c(5, 6, 7))
filter(df, x > 1 & y < 7, z == 2)
AError: unexpected symbol in 'filter(df, x > 1 & y < 7, z == 2)'
BNo error, returns rows where x > 1, y < 7, and z == 2
CError: object 'z' not found
DWarning: z is not a column in df, but filter runs anyway
Attempts:
2 left
💡 Hint
Check if all variables used in filter exist in the data frame.
🧠 Conceptual
advanced
2:00remaining
Understanding filter() with NA values
Given this data frame, what will filter(df, x > 2) return?
R Programming
library(dplyr)
df <- tibble(x = c(1, 2, NA, 4))
result <- filter(df, x > 2)
print(result)
AA tibble with rows where x is 4 only
BA tibble with rows where x is 4 and NA
CAn error because NA cannot be compared
DA tibble with rows where x is 1, 2, and 4
Attempts:
2 left
💡 Hint
Remember how comparisons with NA behave in R.
❓ Predict Output
expert
3:00remaining
Complex filter() with grouped data
What is the output of this R code using filter() on grouped data?
R Programming
library(dplyr)
df <- tibble(group = c('A', 'A', 'B', 'B'), value = c(10, 20, 30, 40))
result <- df %>% group_by(group) %>% filter(value == max(value))
print(result)
AError: max() not found in filter
B
A tibble: 2 × 2
Groups:   group [2]
  group value
  &lt;chr&gt; &lt;dbl&gt;
1 A        20
2 B        40
C
A tibble: 1 × 2
Groups:   group [1]
  group value
  &lt;chr&gt; &lt;dbl&gt;
1 B        40
D
A tibble: 4 × 2
Groups:   group [2]
  group value
  &lt;chr&gt; &lt;dbl&gt;
1 A        10
2 A        20
3 B        30
4 B        40
Attempts:
2 left
💡 Hint
filter() inside group_by keeps rows where the condition is TRUE per group.

Practice

(1/5)
1. What does the filter() function in R do when applied to a data frame?
easy
A. Selects rows that meet specific conditions
B. Selects columns based on their names
C. Sorts the data frame by a column
D. Adds new columns to the data frame

Solution

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

    The filter() function is used to pick rows from a data frame that satisfy certain conditions.
  2. Step 2: Compare with other functions

    Functions like select() pick columns, and arrange() sorts rows, so they do not match filter()'s role.
  3. Final Answer:

    Selects rows that meet specific conditions -> Option A
  4. Quick Check:

    filter() = row selection [OK]
Hint: Remember: filter() picks rows, select() picks columns [OK]
Common Mistakes:
  • Confusing filter() with select()
  • Thinking filter() sorts data
  • Assuming filter() adds columns
2. Which of the following is the correct syntax to filter rows where the column age is greater than 30 using dplyr?
easy
A. filter(data, age => 30)
B. filter(data, age > 30)
C. filter(data, age = 30)
D. filter(data, age < 30)

Solution

  1. Step 1: Check the comparison operator

    The correct operator for 'greater than' is >, so age > 30 is valid.
  2. Step 2: Validate syntax correctness

    filter(data, age > 30) correctly applies the condition inside filter(). Other options use wrong operators or conditions.
  3. Final Answer:

    filter(data, age > 30) -> Option B
  4. Quick Check:

    Use > for greater than in filter() [OK]
Hint: Use >, <, == for conditions inside filter() [OK]
Common Mistakes:
  • Using => instead of >
  • Using = instead of == or >
  • Confusing < with >
3. Given the data frame df below:
df <- data.frame(name = c('Anna', 'Bob', 'Cara'), age = c(25, 35, 30))

What will be the output of filter(df, age >= 30)?
medium
A. All rows
B. Rows with Anna and Bob
C. Rows with Anna and Cara
D. Rows with Bob and Cara

Solution

  1. Step 1: Identify rows where age >= 30

    From the data, Bob has age 35 and Cara has age 30, both satisfy age >= 30.
  2. Step 2: Confirm which rows are selected

    Anna has age 25, which is less than 30, so she is excluded.
  3. Final Answer:

    Rows with Bob and Cara -> Option D
  4. Quick Check:

    age >= 30 selects Bob and Cara [OK]
Hint: Check each row's value against the condition carefully [OK]
Common Mistakes:
  • Including rows with age less than 30
  • Misreading >= as >
  • Confusing row names with values
4. Consider this code snippet:
library(dplyr)
df <- data.frame(score = c(80, 90, 70))
filtered <- filter(df, score => 80)

What is the problem with this code?
medium
A. The operator => is invalid in R
B. The data frame is missing a column
C. filter() needs a pipe operator %>%
D. score should be in quotes

Solution

  1. Step 1: Check the operator used in filter()

    The operator => is not valid in R; the correct operator for 'greater or equal' is >=.
  2. Step 2: Validate other parts of the code

    The data frame has the correct column, filter() can be used without pipe, and column names do not need quotes inside filter().
  3. Final Answer:

    The operator => is invalid in R -> Option A
  4. Quick Check:

    Use >=, not =>, for comparisons [OK]
Hint: Use >= for 'greater or equal' in R, not => [OK]
Common Mistakes:
  • Using => instead of >=
  • Thinking filter() always needs %>%
  • Quoting column names inside filter()
5. You have a data frame df with columns city and temperature. You want to select rows where city is either 'Paris' or 'London' and temperature is above 20. Which filter() command correctly does this?
hard
A. filter(df, city == 'Paris' | city == 'London' & temperature > 20)
B. filter(df, city == 'Paris' & city == 'London' & temperature > 20)
C. filter(df, (city == 'Paris' | city == 'London') & temperature > 20)
D. filter(df, city == 'Paris' || city == 'London' && temperature > 20)

Solution

  1. Step 1: Understand operator precedence in R

    Logical AND (&) has higher precedence than OR (|), so parentheses are needed to group city conditions.
  2. Step 2: Analyze each option's logic

    filter(df, (city == 'Paris' | city == 'London') & temperature > 20) correctly groups city conditions with parentheses and then ANDs with temperature > 20. filter(df, city == 'Paris' | city == 'London' & temperature > 20) lacks parentheses, causing wrong logic. filter(df, city == 'Paris' & city == 'London' & temperature > 20) requires city to be both Paris and London simultaneously, impossible. filter(df, city == 'Paris' || city == 'London' && temperature > 20) uses || and && which are not vectorized and cause errors in filter().
  3. Final Answer:

    filter(df, (city == 'Paris' | city == 'London') & temperature > 20) -> Option C
  4. Quick Check:

    Use parentheses to group OR conditions in filter() [OK]
Hint: Use parentheses to group OR conditions before AND [OK]
Common Mistakes:
  • Missing parentheses causing wrong logic
  • Using && and || instead of & and |
  • Trying city == 'Paris' & city == 'London' together
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