Write R code using dplyr to filter the data frame students for rows where grade is greater than 75, and add a new column called honor which is TRUE if grade is 90 or higher.
The dplyr
package makes it easy to manipulate and transform data frames in R. Common tasks include filtering rows, selecting columns, creating new variables, and summarizing data.
Unfortunately, we cannot use the dplyr library in our online editor at the moment, but the below code snippets show how this would work.
library(dplyr)
# Sample data
df <- data.frame(
name = c("Alice", "Bob", "Charlie", "David"),
age = c(25, 30, 35, 40),
score = c(90, 85, 88, 92)
)
# Filter rows where age > 30 and create a new variable "passed"
df_clean <- df %>%
filter(age > 30) %>%
mutate(passed = score >= 90)
This pipeline filters the data to only include people older than 30 and adds a new column indicating if their score is above 90.