I am trying to convert a set of variables as factored variable :
example in variable quality the values are : 3,4,5,6,7,8,9
I want a new FACTORED variable qual_level which has values low , medium and high such that
low <- quality(3,4)
medium<- quality(5,6,7)
high<- quality(8,9)
Hence I tried implementing following code .
q_levels <- a <- factor (white_wine$quality ,
c(3,4,5.6,7,8,9,10),
levels=1:3,
labels=c("Low", "Medium", "High"))
Above code throws an error :
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
How can I improve the code ?
Use cut to discretise a continuous variable:
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
If you have more levels, cut() is the way to go. But, if you have seven levels only, you can also use fct_collapse() in the forcats package.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With