Skip to content

Instantly share code, notes, and snippets.

@ramhiser
Created February 10, 2017 19:13
Show Gist options
  • Save ramhiser/93fe37be439c480dc26c4bed8aab03dd to your computer and use it in GitHub Desktop.
Save ramhiser/93fe37be439c480dc26c4bed8aab03dd to your computer and use it in GitHub Desktop.
Convert all character columns to factors using dplyr in R
library(dplyr)
iris_char <- iris %>%
mutate(Species=as.character(Species),
char_column=sample(letters[1:5], nrow(iris), replace=TRUE))
sum(sapply(iris_char, is.character)) # 2
iris_factor <- iris_char %>%
mutate_if(sapply(iris_char, is.character), as.factor)
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species char_column
# "numeric" "numeric" "numeric" "numeric" "character" "character"
sapply(iris_factor, class)
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species char_column
# "numeric" "numeric" "numeric" "numeric" "factor" "factor"
@abalter
Copy link

abalter commented Dec 21, 2018

Awesome!

@SumanthLazarus
Copy link

This code colved my problem of column type conversion in a dataframe.
mutate( column_1 = as.character(column_1))

@BobMuenchen
Copy link

Or you could use:
iris_factor <- iris_char %>% type.convert()

@mcstrother
Copy link

Is sapply necessary here? I think
iris_factor <- iris %>% mutate_if(is.character,as.factor)
works just as well.

@davepum
Copy link

davepum commented Jul 14, 2020

Apparently mutate_if (and mutate_all and mutate_at) have been superseded by across:
iris_factor <- iris %>% mutate(across(where(is_character),as_factor))
(although I like mutate_if for this problem).

@Aphale5
Copy link

Aphale5 commented Aug 7, 2020

This helped me a lot! Thank you! 👍

Was struggling a lot with a dataset of 81 variables, was finding a way to convert characters into factors, this is perfect!

@MdAhsanulHimel
Copy link

Is sapply necessary here? I think
iris_factor <- iris %>% mutate_if(is.character,as.factor)
works just as well.

This works fine!

@zoe-xanthellae
Copy link

This worked beautifully, can't believe I didn't have this in my code before! I pull from a oracle database that default assigns every column to either int or chr, and this add-on allows me to do quick QA to make sure all the appropriate rows were pulled and none were dropped. Thank you!

@thaismdr
Copy link

Does this work while using case_when also? Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment