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)
1 thought on “Hack: How to Convert all Character Variables to Factors”
The easy way to do this with dplyr is
`mutate_if(is.character, as.factor)`