Learn R Programming

dataPreparation (version 0.1)

setColAsFactorOrLogical: Set columns as factor

Description

Set columns as factor, or logical if they have too many different values

Usage

setColAsFactorOrLogical(dataSet, cols, n_levels = 53, verbose = TRUE)

Arguments

dataSet

Matrix, data.frame or data.table

cols

a list of colnames of dataSet (or just one) to transform into factor

n_levels

max number of levels for factor (integer, default to 53)

verbose

should the function log (logical, default to TRUE)

Value

dataSet (as a data.table), with specified columns set as factor or logical.

Details

Control number of levels will help you to distinguish true categoricals from just characters that should be handle in another way.

Examples

Run this code
# NOT RUN {
# Load messy_adult
data("messy_adult")

# we wil change mail and education
head(messy_adult[, .(mail, education)])

messy_adult <- setColAsFactorOrLogical(messy_adult, cols = c("mail", "education"))

sapply(messy_adult[, .(mail, education)], class)
head(messy_adult[, .(mail, education)])
# education is now a factor and mail a logical wether there was or not an mail.
# }

Run the code above in your browser using DataLab