我正在尝试将数字划分为多个类别以创建新列。基本上是尝试根据分数创建字母等级(“ 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”
答案 0 :(得分:6)
怎么样
cut(df$score,breaks=c(0,6:10)*10,labels=rev(LETTERS[c(1:4,6)]))
? rev(LETTERS[c(1:4,6)])
可能太聪明了,不能在c("F","D","C","B","A")
上保存 个字符...
答案 1 :(得分:4)
如评论中所建议,您可以使用case_when
:
df_with_grade <- df %>%
mutate(Grade = case_when(score >= 90 ~ "A",
score >= 80 ~ "B",
score >= 70 ~ "C",
score >= 60 ~ "D",
TRUE ~ "F"))
答案 2 :(得分:3)
您不能使用ifelse,它仅适用于二进制条件。像下面那样使用切割,
df$Grade = cut(df$score,
breaks=c(0,60,70,80,90,100),
label=c("F","D","C","B","A"),
include.lowest =TRUE)
答案 3 :(得分:3)
仅为说明您可以使用ifelse
。
df_with_grade <- df %>%
mutate(Grade =
ifelse(score>= 90, "A",
ifelse(score>=80, "B",
ifelse(score>=70, "C",
ifelse(score>=60, "D",
"F"))))
)