0
0
R Programmingprogramming~20 mins

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

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