Я пытаюсь разделить числа на категории, чтобы создать новый столбец. В основном, пытаясь создать буквенную оценку («A», «B», «C», «D», «F») из баллов.
Я воспроизвел похожий фрейм данных на тот, который я 'У меня возникли проблемы в следующем коде.
df <- tibble(score = rnorm(20:100, n = 150))
Код, который я написал для добавления столбца оценок, выглядит следующим образом:
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"
}
)
Код выполняется с предупреждением:
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
В результате все баллы присваиваются "F"