Predictive Hacks

Hack: How to Convert all Character Variables to Factors

characters to factors

Let’s say that we want to convert all Character Variables to Factors and we are dealing with a large data frame of many columns which means that is not practical to convert them one by one. Thus, our approach is to detect the “char” variables and to convert them to “Factors”.

Let’s provide a toy example:

df<-data.frame(Gender = c("F", "F", "M","M","F"), 
               Score  = c(80, 70, 65, 85, 95),
               Type = c("A","B","C","B","B"))
 

As we can see, the Gender and Type are char variables. Let’s convert them to factors.

df[sapply(df, is.character)] <- lapply(df[sapply(df, is.character)], 
                                                           as.factor)
 

As we can see, we managed to convert them. Now, you can also rename and relevel the factors. Notice that we could work in the other way around by converting the Factors to Characters. Generally, we can change different data types.

Other Approaches using Packages

Using dplyr

df <- df%>%mutate_if(is.character, as.factor)

Using the dplyr 1.0.0

df <- df%>%mutate(across(where(is.factor), as.character))
 

Using the purrr package.

library(purrr)

df <- df%>% modify_if(is.factor, as.character) 
 

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

1 thought on “Hack: How to Convert all Character Variables to Factors”

Leave a Comment

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.

Python

Intro to Chatbots with HuggingFace

In this tutorial, we will show you how to use the Transformers library from HuggingFace to build chatbot pipelines. Let’s