I'm trying to cut numbers into categories to create a new column. Basically, trying to create a letter grade ("A", "B", "C", "D", "F") from scores.
I have reproduced a similar data frame to the one I'm having trouble with in the following code.
df <- tibble(score = rnorm(20:100, n = 150))
The code I wrote to add the grade column looks like this:
df_with_grade <- df %>%
mutate(Grade = if (score >= 90) {
"A"
} else if (score >= 80){
"B"
} else if (score >= 70){
"C"
} else if (score >= 60){
"D"
} else {
"F"
}
)
The code executes with a warning:
Warning messages:
1: In if (score >= 90) { :
the condition has length > 1 and only the first element will be used
2: In if (score >= 80) { :
the condition has length > 1 and only the first element will be used
3: In if (score >= 70) { :
the condition has length > 1 and only the first element will be used
4: In if (score >= 60) { :
the condition has length > 1 and only the first element will be used
The result is, all scores are assigned an "F"