Convert Factor to Numeric and Numeric to Factor in R Programming
Last Updated :
23 Apr, 2025
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:
- Convert the data vector into a factor. The factor() command is used to create and modify factors in R.
- 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
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
Similar Reads
Convert a Vector into Factor in R Programming - as.factor() Function
as.factor() function in R Programming Language is used to convert the passed object(usually Vector) into a Factor. Syntax: as.factor(object) Parameters: Object: Vector to be convertedas.factor() Function in R ExampleExample 1: Convert a Factor in R C/C++ Code # Creating a vector x<-c("female
1 min read
Convert an Unordered Factor to an Ordered Factor in R Programming - as.ordered() Function
as.ordered() function in R Language takes an unordered factor as argument and converts it into an ordered factor. Syntax: as.ordered(factor) Parameters: factor: Unordered Factor to be converted Example 1: # Creating a vector x<-c("North", "North", "East", "West
1 min read
Convert an Integer to UTF8 in R Programming - intToUtf8() Function
intToUtf8() function in R Language is used to convert an integer into UTF8 value. Syntax: intToUtf8(x, multiple) Parameters: x: Integer or integer vector multiple: Boolean value to convert into single or multiple strings Example 1: # R program to convert an integer to UTF8 # Calling the intToUtf8()
1 min read
Check if an Object is of Type Numeric in R Programming - is.numeric() Function
is.numeric() function in R Language is used to check if the object passed to it as argument is of numeric type. Syntax: is.numeric(x) Parameters: x: Object to be checked Example 1: # R program to check if # object is of numeric type # Calling is.numeric() function is.numeric(1) is.numeric(1.5) is.nu
1 min read
Convert a Data Frame into a Numeric Matrix in R Programming - data.matrix() Function
data.matrix() function in R Language is used to create a matrix by converting all the values of a Data Frame into numeric mode and then binding them as a matrix. Syntax: data.matrix(df) Parameters: df: Data frame to be converted. Example 1: # R program to convert a data frame # into a numeric matrix
2 min read
Convert a Numeric Object to Character in R Programming - as.character() Function
as.character() function in R Language is used to convert a numeric object to character object. Syntax: as.character(x) Parameters: x: Numeric Object Example 1: # R program to convert a numeric object # to character object # Calling as.character() function as.character(1) as.character(2 + 3) as.chara
1 min read
Convert an Integer to Bits in R Programming - intToBits() Function
intToBits() function in R Language is used to convert an integer into Bits i.e. 0s and 1s. The output is of length 32 times the length of integer vector. Syntax: intToBits(x) Parameters: x: Integer or integer vector Example 1: # R program to convert an integer to bits # Calling the intToBits() funct
2 min read
Convert a Data Frame into a Molten Form in R Programming - melt() Function
function in R Language is used to combine multiple columns of s Data Frame into a single column. Syntax: melt(x, na.rm, value.name) Parameters: x: data to be melted na.rm: Boolean value to remove NA value.name: Setting column names Example 1: # R program to reshape data frame # Loading library libra
2 min read
Convert an Object into a Vector in R Programming - as.vector() Function
as.vector() function in R Language is used to convert an object into a vector. Syntax: as.vector(x) Parameters: x: Object to be converted Example 1: # R program to convert an object to vector # Creating an array x <- array(c(2, 3, 4, 7, 2, 5), c(3, 2)) x # Calling as.vector() Function as.vector(x
1 min read
Convert type of data object in R Programming - type.convert() Function
type.convert() function in R Language is used to compute the data type of a particular data object. It can convert data object to logical, integer, numeric, or factor. Syntax: type.convert(x) Parameter: x: It can be a vector matrix or an array Example 1: Apply type.convert to vector of numbers # R p
2 min read