我试图将一组变量转换为因子变量:
可变质量的示例值为:3,4,5,6,7,8,9
我想要一个新的FACTORED变量qual_level,其值为low,medium和high,以便
low <- quality(3,4)
medium<- quality(5,6,7)
high<- quality(8,9)
因此我尝试实现以下代码。
q_levels <- a <- factor (white_wine$quality ,
c(3,4,5.6,7,8,9,10),
levels=1:3,
labels=c("Low", "Medium", "High"))
上面的代码会抛出错误:
Error in factor(white_wine$quality, c(3, 4, 5.6, 7, 8, 9, 10), levels = 1:3, : invalid 'labels'; length 3 should be 1 or 2
如何改进代码?
答案 0 :(得分:7)
使用cut
来区分连续变量:
x <- c(3,4,5,6,7,8,9)
cut(x, c(-Inf, 4, 7, 9), labels = c("Low", "Medium", "High"))
#[1] Low Low Medium Medium Medium High High
#Levels: Low Medium High
答案 1 :(得分:1)
如果您有更多级别,cut()
是可行的方法。但是,如果您只有七个级别,则还可以在fct_collapse()
包中使用forcats
。
library(forcats)
quality <- factor(3:9)
fct_collapse(quality,
low = c("3", "4"),
medium = c("5", "6", "7"),
high = c("8", "9"))
#[1] low low medium medium medium high high
#Levels: low medium high