R : Converting Multiple Numeric Variables to Factor

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)
     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)
    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)
Spread the Word!
Related Posts
About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he has worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and Human Resource.

8 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...

  2. these do not work

  3. Thank you, it was what I was looking for!

  4. I believe that in 5. the right code for col_names is:

    col_names <- sapply(mydata,
    function(col) {length(unique(col) < 4} )

  5. I followed your data type conversion example on my Excel ".xlsx" file. The numeric columns were converted into factors which is required by the package that I am using. However, when I run the R package, I get an error that goes like this: Error in '$<- .data.frame.'(*tmp*', "Trt", value = character(0)) replacement has 0 rows, data has 20.

    When I check the data type conversion using str() function, the numeric columns were converted to factors as I desired. However, it seems that the "myData[, names]" statement did not capture any of the data rows in the dataframe when in fact it should.

    Any helpful thoughts about my problem?

    Thank you.

  6. This comment has been removed by the author.

  7. Hi

    Can you please clarify that variables like exposuretime, size, concentration should be included in the generalized linear model as numeric or factors? Thanks


Next → ← Prev
Love this post? Support Us!
Buy Me A Coffee