-1

enter image description here

I would like to have the price range field to display as factor with 4 levels where 0=poor, 1=Normal, 2=Medium and 3=High. I have managed to create few other fields as factor with 2 levels. But don't know how to create a field as factor with 4 levels? The data set is names as mobile_price_classification The uploaded screenshot is after the command:

> str(mobile_price_classification)

I want to have the price_range field as factor with 4 levels, where 0=poor, 1=Normal, 2=Medium and 3=High.

Phil
  • 7,287
  • 3
  • 36
  • 66
Jack432
  • 13
  • 1
  • 2
    Please do not post screenshots of code or data. Provide the output of `dput(mobile_price_classification)` instead. – Phil Mar 16 '23 at 22:21
  • Hi, please provide a reproducible example as @Phil has noted: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – william3031 Mar 16 '23 at 22:23
  • I am really sorry. I don't know how to provide a reproducible example :( – Jack432 Mar 16 '23 at 22:30
  • @Jack432 no need to be sorry just click William's link and follow the instructions there. I tried to help by making an example below. For starters following Phil's instructions and paste the result into your question will provide your data for others to troubleshoot on.. – dandrews Mar 17 '23 at 02:59

2 Answers2

1

Maybe this helps:

library(tidyverse)
df <- data.frame( price_range=sample(seq(0,3,1),20,replace = T),
                  x=rnorm(20,10,1))
df2 <- df %>% 
  mutate(price_range=case_when(price_range==0 ~ 'poor',
                               price_range==1 ~ 'normal',
                               price_range==2 ~ 'medium',
                               price_range==3 ~ 'high'),
         price_range=factor(price_range, levels = c('poor','normal','medium','high')))
df2
dandrews
  • 967
  • 5
  • 18
0

It is possible to convert integer values directly into the factor labels using the factor() function.

df <- data.frame( price_range=sample(seq(0,3,1),20,replace = T),
                  x=rnorm(20,10,1))
df$price_range<- factor(df$price_range, labels = c('poor','normal','medium','high'))

df$price_range
[1] poor   poor   high   medium normal high   medium normal medium normal normal high   normal normal normal medium poor   poor   medium
[20] medium
Levels: poor normal medium high

Depending on your analysis you may want to consider adding the "order=TRUE" to the function call. See ?factor for more information.

Dave2e
  • 22,192
  • 18
  • 42
  • 50