Hello, readers! In this article, we will be focusing on the way to Convert Factor to Numeric and Numeric to Factor in R programming, in detail.
So, let us begin! 🙂
Prior to getting started with the conversions, let us first understand briefly about Factors and Numeric types.
Factors are the data types that represent the categorical data values. That is they represent the group data values of categorical format values altogether. It stores multiple levels of data of different categories at a single instance.
For example: The categories like Male and Female can be represented by categories or levels by Factors.
These factor values can also be represented by levels of integer values.
Let us now have a look at the conversion of factor to numeric and vice-versa in the upcoming section.
1. Conversion of Factor to Numeric format in R
While taking factors into consideration, when it comes to conversion of factors, we can come across the below options–
- Conversion of factor to characters: At this time, when the factor data contains numbers as levels(groups), we first need to convert the factor data into character using
as.character()function, and then convert the character data into numeric format using
- Conversion of factor to numeric type
Let us take both of the above scenarios into consideration.
R as.numeric() function to convert factor to numeric form
With as.numeric() function, we can convert the factor type values into a numeric form. That is, it translates the representation of levels of the categories to a numeric form easily.
In this example, we have initially created a vector using c() function. Further, we have converted the vector to factor form using factor() function.
After which, we apply the as.numeric() function and convert the category/levels of the factor data into numeric format.
rm(list = ls()) data = c("Yes","No","May be") print(data) data = factor(data) print(as.numeric(data))
> print(data)  "Yes" "No" "May be" > print(as.numeric(data))  3 2 1
2. Conversion of Numeric to Factor format
cut() function helps us to convert the numeric form of data into Factor format. To do so, it divides the range of the numeric data format into intervals and obtains the values according the corresponding interval they fall into.
cut.default(x, breaks, labels = NULL, include.lowest = FALSE)
- x: numeric data
- breaks: If the value is provided, the entire range of numeric data gets divided into this ‘breaks’.
- include.lowest: If set to True, it include the lowest value in the consideration
In this example, we have created a dataframe with a numeric column ‘roll’ and a factor column ‘Poll’ using data.frame() function. Further, we have used cut() function to get three factor sections of the numeric column ‘roll’.
At last, we use table() function to represent the factor data.
rm(list = ls()) roll <- c(10,20,30,40,50) Poll <- c("Yes", "No", "May BE","May be Yes", "May be NO") data<- data.frame(roll,Poll) wfact=cut(data$roll, 3) table(wfact)
wfact (9.96,23.3] (23.3,36.7] (36.7,50] 2 1 2
By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.
For more such posts related to R programming, Stay tuned with us.
Till then, Happy Learning!! 🙂