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"))
 
Hack: How to Convert all Character Variables to Factors 1

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)
 
Hack: How to Convert all Character Variables to Factors 2

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

snowflake
Miscellaneous

How to Schedule Tasks in Snowflake

We have started a series of Snowflake tutorials, like How to Get Data from Snowflake using Python, How to Load