3

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 ?

camille
  • 16,432
  • 18
  • 38
  • 60
Kinjal Kachi
  • 111
  • 1
  • 1
  • 8

2 Answers2

7

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
Roland
  • 127,288
  • 10
  • 191
  • 288
1

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  
jazzurro
  • 23,179
  • 35
  • 66
  • 76
  • @Sotos Not that weird. Factors, by their very nature, are *discrete values*, not (continuous) numbers. – Konrad Rudolph Sep 18 '17 at 12:53
  • @KonradRudolph I guess as a mathematician I m used to interpret discrete values/Factors as pure numbers. – Sotos Sep 18 '17 at 12:58
  • @Sotos The difference is that mathematics is *untyped*. Everything is numbers (or sets, or ‹insert abstraction here›). Programming languages are typed (even if, as in the case of R, the typing is rather weak and reluctant), and the types are actually rather important. – Konrad Rudolph Sep 18 '17 at 12:59
  • @KonradRudolph Exactly my point (I was not familiar with the term *typed/untyped*) – Sotos Sep 18 '17 at 13:02