-
-
Save ramhiser/93fe37be439c480dc26c4bed8aab03dd to your computer and use it in GitHub Desktop.
| 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" |
This code colved my problem of column type conversion in a dataframe.
mutate( column_1 = as.character(column_1))
Or you could use:
iris_factor <- iris_char %>% type.convert()
Is sapply necessary here? I think
iris_factor <- iris %>% mutate_if(is.character,as.factor)
works just as well.
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).
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!
Is
sapplynecessary here? I think
iris_factor <- iris %>% mutate_if(is.character,as.factor)
works just as well.
This works fine!
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!
Does this work while using case_when also? Thanks :)
Thanks a lot! I had a similar problem. It works beautyfully , but there was a complaint that mutate_if is deprecated. I changed it to the new syntax:
iris_factor <- iris %>% mutate(across(where(is.character), as.factor))
Awesome!