0
0
R Programmingprogramming~20 mins

Filtering rows in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Filtering Rows Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of filtering rows with dplyr filter()
What is the output of this R code that filters rows where score is greater than 80?
R Programming
library(dplyr)
data <- data.frame(name = c("Anna", "Ben", "Cara", "Dan"), score = c(75, 85, 90, 60))
filtered <- filter(data, score > 80)
print(filtered)
AError: object 'filter' not found
B
  name score
1  Anna    75
2  Ben    85
3 Cara    90
4  Dan    60
C
  name score
1  Anna    75
2  Dan    60
D
  name score
1  Ben    85
2 Cara    90
Attempts:
2 left
💡 Hint
Look for rows where score is more than 80.
Predict Output
intermediate
2:00remaining
Filtering rows with base R subset()
What will this R code print after filtering rows where age is less than or equal to 30?
R Programming
data <- data.frame(name = c("Eva", "Frank", "Gina", "Hank"), age = c(25, 35, 30, 40))
result <- subset(data, age <= 30)
print(result)
A
  name age
1  Eva  25
3 Gina  30
B
  name age
2 Frank 35
4 Hank  40
CError in subset(data, age <= 30): object 'age' not found
D
  name age
1  Eva  25
2 Frank 35
3 Gina  30
4 Hank  40
Attempts:
2 left
💡 Hint
subset() keeps rows where the condition is TRUE.
Predict Output
advanced
2:00remaining
Filtering rows with multiple conditions
What is the output of this R code filtering rows where score is above 70 and grade is 'A'?
R Programming
library(dplyr)
data <- data.frame(name = c("Ivy", "Jack", "Kara", "Liam"), score = c(80, 90, 65, 85), grade = c("B", "A", "A", "A"))
filtered <- filter(data, score > 70 & grade == "A")
print(filtered)
A
  name score grade
1 Jack    90     A
2 Liam    85     A
B
  name score grade
1 Ivy     80     B
2 Jack    90     A
3 Liam    85     A
C
  name score grade
1 Kara    65     A
D
  name score grade
1 Ivy     80     B
Attempts:
2 left
💡 Hint
Both conditions score > 70 and grade == 'A' must be true.
Predict Output
advanced
2:00remaining
Filtering rows with NA values
What will this R code print after filtering rows where value is not NA?
R Programming
data <- data.frame(id = 1:5, value = c(10, NA, 20, NA, 30))
filtered <- subset(data, !is.na(value))
print(filtered)
A
  id value
2  2    NA
4  4    NA
B
  id value
1  1    10
2  2    NA
3  3    20
4  4    NA
5  5    30
C
  id value
1  1    10
3  3    20
5  5    30
DError in subset(data, !is.na(value)): object 'value' not found
Attempts:
2 left
💡 Hint
Use !is.na() to keep rows without missing values.
Predict Output
expert
3:00remaining
Filtering rows with complex condition and mutate
What is the output of this R code that filters rows where new_score is above 50 after mutation?
R Programming
library(dplyr)
library(magrittr)
data <- data.frame(name = c("Mia", "Ned", "Oli", "Pam"), score = c(40, 60, 55, 45))
result <- data %>% mutate(new_score = score * 1.2) %>% filter(new_score > 50)
print(result)
A
  name score new_score
1  Mia    40        48
2  Ned    60        72
3  Oli    55        66
4  Pam    45        54
B
  name score new_score
1  Ned    60        72
2  Oli    55        66
C
  name score new_score
1  Mia    40        48
4  Pam    45        54
DError: could not find function "%>%"
Attempts:
2 left
💡 Hint
First multiply score by 1.2, then keep rows where new_score > 50.