R:将字母等级转换为因子

时间:2018-06-27 18:01:13

标签: r

所以我有一组等级从A +到F的数据(还有99个不知道为什么会这样)

我要做的是将A +〜B0转换为“高”,将C +〜F转换为“低”

我有一列“结果”,其中包含字母数据列表。

我将另一列“等级”创建为因素“高”,“低”

raw.tidy$grade <- raw.tidy$result(high = c("A+", "A0", "B+", "B0"),
                                  low = c("C+", "C0", "D+", "D0", "F", "99"))

这是我正在使用的代码,但这给了 错误:尝试应用非功能 错误。

我检查了其他问题,可以将A +转换为A +,将C0转换为C0,但我不明白如何将A +和A0转换为“高”。

3 个答案:

答案 0 :(得分:1)

您可以使用case_when()软件包中的dplyr函数。

library(dplyr)

df <- tibble(
  result = c("A+", "B0", "D+", "F", "A0", "99", "C+", "A+", "C0", "D0")
)

df %>% 
  mutate(grade = factor(case_when(
    result %in% c("A+", "A0", "B+", "B0") ~ "high",
    result %in% c("C+", "C0", "D+", "D0", "F", "99") ~ "low"
  )))

# A tibble: 10 x 2
   result grade
   <chr>  <fct>
 1 A+     high 
 2 B0     high 
 3 D+     low  
 4 F      low  
 5 A0     high 
 6 99     low  
 7 C+     low  
 8 A+     high 
 9 C0     low  
10 D0     low  

有关case_when的更多信息,请参考tidyverse.org网站上的documentation

答案 1 :(得分:1)

您可以使用findInterval函数:

vec = c("D+", "A0", "F", "D0", "99", "B0", "B+", "C+", "C0", "A+")

c("High","Low")[findInterval(vec>"C"|vec=="99",0:1)]

[1] "Low"  "High" "Low"  "Low"  "Low"  "High" "High" "Low"  "Low"  "High"

甚至

factor(findInterval(vec>"C" | vec == "99", 0:1 ), label = c("High","Low"))

[1] Low  High Low  Low  Low  High High Low  Low  High
Levels: High Low

如果有数据框:

df = data.frame(result=vec,stringsAsFactors=FALSE)

transform(df,grade=factor(findInterval(result>"C"|result=="99",0:1),label=c("High","Low")))
   result grade
1      D+   Low
2      A0  High
3       F   Low
4      D0   Low
5      99   Low
6      B0  High
7      B+  High
8      C+   Low
9      C0   Low
10     A+  High

您可以看到Demo here

答案 2 :(得分:0)

以R为底

high = c("A+", "A0", "B+", "B0")

low = c("C+", "C0", "D+", "D0", "F", "99")


df$grade <- factor(ifelse(df$result %in% high, "high","low"))