Open In App

Convert Factor to Numeric and Numeric to Factor in R Programming

Last Updated : 23 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Factors are data structures that are implemented to categorize the data or represent categorical data and store it on multiple levels. They can be stored as integers with a corresponding label to every unique integer. Though factors may look similar to character vectors, they are integers and care must be taken while using them as strings. The factor accepts only a restricted number of distinct values. It is helpful in categorizing data and storing it on multiple levels.

Converting Factors to Numeric Values

In R we require to change factors to either numbers or text. To achieve this, one has to use the functions as.character() or as.numeric(). There are two steps for converting a factor to a numeric:

  1. Convert the data vector into a factor. The factor() command is used to create and modify factors in R.
  2. The factor is converted into a numeric vector using as.numeric(). When a factor is converted into a numeric vector, the numeric codes corresponding to the factor levels will be returned.

Converting a Factor that is a Number.

If the factor is a number, first convert it to a character vector and then to a numeric. If a factor is a character then you need not convert it to a character. And if you try converting an alphabet character to numeric it will return NA.

Example 1:

We are creating a factor of numeric values (soap_cost), converting it to numeric by first coercing it to character, then back to numeric. Finally, we check if the result is numeric.

R
soap_cost <- factor(c(29, 28, 210, 28, 29))

a1<-as.numeric(as.character(soap_cost))

a1

is.numeric(a1)

Output:

[1] 29 28 210 28 29

[1] TRUE

Factor-to-numeric

Factor to Numeric

However, if you simply use as. numeric(), the output is a vector of the internal level representations of the factor and not the original values. 

Example 2:

R
soap_cost <- factor(c(29, 28, 210, 28, 29))

a1<-as.numeric(soap_cost)

a1

is.numeric(a1)

Output:

[1] 2 1 3 1 2

[1] TRUE

Converting Numeric value to a Factor

For converting a numeric into a factor we use the cut() function. cut() divides the range of numeric vector(assume x) which is to be converted by cutting into intervals and codes its value (x) according to which interval they fall. Level one corresponds to the leftmost, level two corresponds to the next leftmost and so on.

Syntax: cut.default(x, breaks, labels = NULL, include.lowest = FALSE, right = TRUE, dig.lab = 3)

Where:

  • When a number is given through the ‘break=’ argument, the output factor is created by the division of the range of variables into that number of equal-length intervals.
  • In syntax include.lowest indicates whether an ‘x[i]’ which equals the lowest (for right= TRUE) break’s value should be included. And ‘right’ in the syntax indicates whether the intervals should be open on the left and closed on the right or vice versa.
  • If labels are not provided then dig. lab is used. The number of digits used in formatting the break numbers is determined through it.

We will also use ‘norm()‘ for generating multivariate normal distributed random variants within the specified space.

Syntax: norm(n, mean, sd)

Where:

  • n: Number of random variables that need to be generated.
  • mean: Its value is 0 by default if not mentioned.
  • sd: standard deviation value needs to be mentioned otherwise it is 1 by default.

Example 1:

We are creating an employee dataset and using cut() to divide the age variable into 3 equal-width intervals (without assigning labels). We then generate a frequency table of these age intervals and check if the output is a factor.

R
age <- c(40, 49, 48, 40, 67, 52, 53)
salary <- c(103200, 106200, 150200, 10606, 10390, 14070, 10220)
gender <- c("male", "male", "transgender",
            "female", "male", "female", "transgender")

employee<- data.frame(age, salary, gender)


wfact = cut(employee$age, 3)
table(wfact)

is.factor(wfact)

Output:

wfact
(40,49] (49,58] (58,67]
4 2 1

[1] TRUE

Example 2:

We are creating a data frame of employee details and using the cut() function to categorize ages into 3 labeled groups: Young, Medium and Aged. Then, we generate a frequency table of these age categories and check if the result is a factor.

R
age <- c(40, 49, 48, 40, 67, 52, 53)
salary <- c(103200, 106200, 150200, 10606, 10390, 14070, 10220)
gender <- c("male", "male", "transgender",
            "female", "male", "female", "transgender")

employee<- data.frame(age, salary, gender)

wfact = cut(employee$age, 3, labels=c('Young', 'Medium', 'Aged'))
table(wfact)

is.factor(wfact)

Output:

wfact
Young Medium Aged
4 2 1
[1] TRUE

Example 3:

We are generating 100 random numbers from a normal distribution and categorizing them into intervals using the cut() function. We then create a frequency table with table() and check if the result is numeric using is.numeric().

R
y <- rnorm(100)

a1<-table(cut(y, breaks = pi/3*(-3:3)))
a1

is.numeric(a1)

Output:

(-3.14,-2.09] (-2.09,-1.05] (-1.05,0] (0,1.05] (1.05,2.09] (2.09,3.14]
0 10 39 31 19 1
[1] TRUE

The output factor is created by the division of the range of variables into 5 equal-length intervals through break argument. 

Example 4:

We are using the cut() function to divide the age variable into 5 equal-width intervals. The table() function is then used to count how many employees fall into each of these intervals. This allows us to see the distribution of employee ages across the defined age groups.

R
age <- c(40, 49, 48, 40, 67, 52, 53)
gender <- c("male", "male", "transgender", "female", "male", "female",
            "transgender")


employee<- data.frame(age, gender)

wfact = cut(employee$age, breaks=5)
table(wfact)

Output:

wfact
(40,45.4] (45.4,50.8] (50.8,56.2] (56.2,61.6] (61.6,67]
2 2 2 0 1



Next Article
Article Tags :
Practice Tags :

Similar Reads