Description Usage Arguments Value Examples
View source: R/all_chr_to_factor.R
A convenience function to convert all character variables in a data frame to factor variables.
1 |
data |
A data frame. |
A data frame with character variables converted to factors.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 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
# Convert all character variables to strings
df2 <- all_chr_to_factor(df)
# Check the classes in each column
mapply(class, df2)
# # Works with tibbles too
# tbl <- tibble::as.tibble(df)
# tbl2 <- all_chr_to_factor(tbl)
# mapply(class, tbl2)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# v0.3.1:
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.