R : Converting Multiple Numeric Variables to Factor


R Data Science: R Programming A-Z: R For Data Science With Real Exercises!

In R, you can convert multiple numeric variables to factor using lapply function. The lapply function is a part of apply family of functions. They perform multiple iterations (loops) in R. In R, categorical variables need to be set as factor variables. Some of the numeric variables which are categorical in nature need to be transformed to factor so that R treats them as a grouping variable.

Converting Numeric Variables to Factor
  1. Using Column Index Numbers
In this case, we are converting first, second, third and fifth numeric variables to factor variables. mydata is a data frame.
names <- c(1:3,5)
mydata[,names] <- lapply(mydata[,names] , factor)
str(mydata)
     2. Using Column Names


In this case, we are converting two variables 'Credit' and 'Balance' to factor variables.
names <- c('Credit' ,'Balance')
mydata[,names] <- lapply(mydata[,names] , factor)
str(mydata)
    3.  Converting all variables
col_names <- names(mydata)
mydata[,col_names] <- lapply(mydata[,col_names] , factor)
    4.  Converting all numeric variables
mydata[sapply(mydata, is.numeric)] <- lapply(mydata[sapply(mydata, is.numeric)], as.factor)
    5. Checking unique values in a variable and convert to factor only those variables having unique count less than 4
col_names <- sapply(mydata, function(col) length(unique(col)) < 4)
mydata[ , col_names] <- lapply(mydata[ , col_names] , factor)
Coursera Data Science

R Tutorials : 75 Free R Tutorials

Get Free Email Updates :
*Please confirm your email address by clicking on the link sent to your Email*

Related Posts:

2 Responses to "R : Converting Multiple Numeric Variables to Factor"

  1. wow.... thank you so much for this. i've been searching for this all over the internet and finally found it here...

    ReplyDelete
  2. these do not work

    ReplyDelete

Next → ← Prev