作者:chajian005_844 | 来源:互联网 | 2023-01-17 23:33
我试图将一组变量转换为因子变量:变量质量的例子值是:3,4,5,6,7,8,9
我想要一个新的FACTORED变量qual_level,它具有低,中,高的值,
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
我该如何改进代码?
1> Roland..:
用于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