A convenience function to convert all character variables in a data frame to factor variables.
all_chr_to_factor(data)
data | A data frame. |
---|
A data frame with character variables converted to factors.
library(biostat) data(PlantGrowth) # Basic syntax PlantGrowth <- all_chr_to_factor(PlantGrowth) # Update the same object using operator from `magrittr` package library(magrittr) PlantGrowth %<>% all_chr_to_factor() # Create a data frame df <- data.frame( letters = letters[1:5], letters2 = LETTERS[1:5], stringsAsFactors = FALSE ) mapply(class, df) # show classes of columns#> letters letters2 #> "character" "character"# Convert all character variables to strings df2 <- all_chr_to_factor(df) # Check the classes in each column mapply(class, df2)#> letters letters2 #> "factor" "factor"# # Works with tibbles too # tbl <- tibble::as.tibble(df) # tbl2 <- all_chr_to_factor(tbl) # mapply(class, tbl2) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # v0.3.1: