Learn R Programming

dataPreparation (version 0.4.3)

setColAsFactor: Set columns as factor

Description

Set columns as factor and control number of unique element, to avoid having too large factors.

Usage

setColAsFactor(dataSet, cols = "auto", n_levels = 53, verbose = TRUE)

Arguments

dataSet

Matrix, data.frame or data.table

cols

List of column(s) name(s) of dataSet to transform into factor. To transform all columns set it to "auto", (characters, default to auto).

n_levels

Max number of levels for factor (integer, default to 53) set it to -1 to disable control.

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 categorical columns from just characters that should be handled in another way.

Examples

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

# we wil change education
messy_adult <- setColAsFactor(messy_adult, cols = "education")

sapply(messy_adult[, .(education)], class)
# education is now a factor
# }

Run the code above in your browser using DataLab