# How to convert a numerical column into factor column in R?

R ProgrammingServer Side ProgrammingProgramming

Often, we find that the values that represent factor levels are recorded as numerical values, therefore, we need to convert those numerical values to factor. In this way, we can use the factor column properly in our analysis otherwise R program will treat the factors as numerical values and the analysis output will be incorrect.

## Example

Live Demo

data(mtcars)
str(mtcars)

## Output

'data.frame': 32 obs. of 11 variables:
$mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...$ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
$disp: num 160 160 108 258 360 ...$ hp : num 110 110 93 110 175 105 245 62 95 123 ...
$drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...$ wt : num 2.62 2.88 2.32 3.21 3.44 ... $qsec: num 16.5 17 18.6 19.4 17 ...$ vs : num 0 0 1 1 0 1 0 1 1 1 ...
$am : num 1 1 1 0 0 0 0 0 0 0 ...$ gear: num 4 4 4 3 3 3 3 4 4 4 ...
$carb: num 4 4 1 1 2 1 4 2 2 4 ... mtcars$cyl<-as.factor(mtcars$cyl) str(mtcars) 'data.frame': 32 obs. of 11 variables:$ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
$cyl : Factor w/ 3 levels "4","6","8": 2 2 1 2 3 2 3 1 1 2 ...$ disp: num 160 160 108 258 360 ...
$hp : num 110 110 93 110 175 105 245 62 95 123 ...$ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
$wt : num 2.62 2.88 2.32 3.21 3.44 ...$ qsec: num 16.5 17 18.6 19.4 17 ...
$vs : num 0 0 1 1 0 1 0 1 1 1 ...$ am : num 1 1 1 0 0 0 0 0 0 0 ...
$gear: num 4 4 4 3 3 3 3 4 4 4 ...$ carb: num 4 4 1 1 2 1 4 2 2 4 ...

## Example

Live Demo

data(ToothGrowth)
str(ToothGrowth)

## Output

'data.frame': 60 obs. of 3 variables:
$len : num 4.2 11.5 7.3 5.8 6.4 10 11.2 11.2 5.2 7 ...$ supp: Factor w/ 2 levels "OJ","VC": 2 2 2 2 2 2 2 2 2 2 ...
$dose: num 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ... ## Example Live Demo head(ToothGrowth,20) ## Output  len supp dose 1 4.2 VC 0.5 2 11.5 VC 0.5 3 7.3 VC 0.5 4 5.8 VC 0.5 5 6.4 VC 0.5 6 10.0 VC 0.5 7 11.2 VC 0.5 8 11.2 VC 0.5 9 5.2 VC 0.5 10 7.0 VC 0.5 11 16.5 VC 1.0 12 16.5 VC 1.0 13 15.2 VC 1.0 14 17.3 VC 1.0 15 22.5 VC 1.0 16 17.3 VC 1.0 17 13.6 VC 1.0 18 14.5 VC 1.0 19 18.8 VC 1.0 20 15.5 VC 1.0 ToothGrowth$dose<-as.factor(ToothGrowth$dose) str(ToothGrowth) 'data.frame': 60 obs. of 3 variables:$ len : num 4.2 11.5 7.3 5.8 6.4 10 11.2 11.2 5.2 7 ...
$supp: Factor w/ 2 levels "OJ","VC": 2 2 2 2 2 2 2 2 2 2 ...$ dose: Factor w/ 3 levels "0.5","1","2": 1 1 1 1 1 1 1 1 1 1 ...

## Example

Live Demo

data(morley)
str(morley)

## Output

'data.frame': 100 obs. of 3 variables: $Expt : int 1 1 1 1 1 1 1 1 1 1 ...$ Run : int 1 2 3 4 5 6 7 8 9 10 ...
$Speed: int 850 740 900 1070 930 850 950 980 980 880 ... ## Example Live Demo morley$Expt<-as.factor(morley$Expt) str(morley) ## Output 'data.frame': 100 obs. of 3 variables:$ Expt : Factor w/ 5 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
$Run : int 1 2 3 4 5 6 7 8 9 10 ...$ Speed: int 850 740 900 1070 930 850 950 980 980 880 ...

## Example

Live Demo

x1<-sample(1:4,20,replace=TRUE) x2<-rnorm(20,2,3)
df<-data.frame(x1,x2)
str(df)

## Output

'data.frame':20 obs. of 2 variables:
$x1: int 1 4 1 2 3 1 4 1 4 2 ...$ x2: num 1.56 1.64 2.83 2.2 4.23 ...

## Example

df$x1<-as.factor(df$x1)
str(df)

## Output

'data.frame': 20 obs. of 2 variables:
$x1: Factor w/ 4 levels "1","2","3","4": 4 3 2 2 4 4 1 2 4 2 ...$ x2: num 3.82 1.13 2.99 5.8 3.3 ...

## Example

Live Demo

data(BOD)
str(BOD) '

## Output

data.frame': 6 obs. of 2 variables:
$Time : num 1 2 3 4 5 7$ demand: num 8.3 10.3 19 16 15.6 19.8 - attr(*, "reference")= chr "A1.4, p. 270"

## Example

Live Demo

BOD$Time<-as.factor(BOD$Time)
str(BOD)

## Output

'data.frame': 6 obs. of 2 variables:
$Time : Factor w/ 6 levels "1","2","3","4",..: 1 2 3 4 5 6$ demand: num 8.3 10.3 19 16 15.6 19.8 - attr(*, "reference")= chr "A1.4, p. 270"
Published on 08-Oct-2020 14:44:01