dummy_code: Dummy-code (one-hot) factors

Description Usage Arguments Details Value TODO See Also Examples

View source: R/data_prep.R

Description

Dummy coding expands a polytomous categorical factor into several indicator variables, each of which is logical (Boolean, TRUE/FALSE).

Usage

1
dummy_code(.data)

Arguments

.data

data frame

Details

The one-hot contrast expands a factor with L levels into L indicator variables. The result is not a full-rank parameterisation, however it avoids needing to select a reference level.

Ordinal (ordered categorical) and dichotomous (only 2 levels) variables are not modified; nor are non-factors (numeric, logical, etc.).

Note that the number of levels is determined from the unique values in the actual data, rather than the factor levels.

The new indicator variables are appended to the end of the variable list, and the original factors are removed.

Value

data frame with selected factors converted to logical dummy variables

TODO

See Also

Other data munging: drop_nzv(), ord_collapse()

Examples

1
2
library(dplyr)
mtcars %>% mutate(across(c("cyl", "gear"), as.factor)) %>% dummy_code()

anchorlytics/anchorUtils documentation built on Oct. 16, 2020, 7:11 p.m.