all_chr_to_factor: *!!* Convert all character variables to factors

Description Usage Arguments Value Examples

View source: R/all_chr_to_factor.R

Description

A convenience function to convert all character variables in a data frame to factor variables.

Usage

1

Arguments

data

A data frame.

Value

A data frame with character variables converted to factors.

Examples

 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:

GegznaV/BioStat documentation built on Aug. 14, 2020, 9:30 p.m.