因子变量:

Kin*_*chi 3 r

我试图将一组变量转换为因子变量:变量质量的例子值是:3,4,5,6,7,8,9 我想要一个新的FACTORED变量qual_level,它具有低,中,高的值,

low <- quality(3,4)
medium<- quality(5,6,7)
high<- quality(8,9)
Run Code Online (Sandbox Code Playgroud)

因此我尝试实现以下代码.

q_levels <-  a <- factor (white_wine$quality ,
                      c(3,4,5.6,7,8,9,10), 
                      levels=1:3, 
                      labels=c("Low",  "Medium", "High"))
Run Code Online (Sandbox Code Playgroud)

上面的代码抛出一个错误:

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
Run Code Online (Sandbox Code Playgroud)

我该如何改进代码?

Rol*_*and 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
Run Code Online (Sandbox Code Playgroud)