Skip to contents

This function takes a data.frame and a categorical variable, performs one-hot encoding, and column-binds the encoded variables back to the data.frame.

Usage

enw_one_hot_encode_feature(metaobs, feature, contrasts = FALSE)

Arguments

metaobs

A data.frame containing the data to be encoded.

feature

The name of the categorical variable to one-hot encode as a character string.

contrasts

Logical. If TRUE, create one-hot encoded variables with contrasts; if FALSE, create them without contrasts. Defaults to FALSE.

See also

Examples

metaobs <- data.frame(week = 1:2)
enw_one_hot_encode_feature(metaobs, "week")
#>     week week1 week2
#>    <int> <num> <num>
#> 1:     1     1     0
#> 2:     2     0     1
enw_one_hot_encode_feature(metaobs, "week", contrasts = TRUE)
#>     week week2
#>    <int> <num>
#> 1:     1     0
#> 2:     2     1

metaobs <- data.frame(week = 1:6)
enw_one_hot_encode_feature(metaobs, "week")
#>     week week1 week2 week3 week4 week5 week6
#>    <int> <num> <num> <num> <num> <num> <num>
#> 1:     1     1     0     0     0     0     0
#> 2:     2     0     1     0     0     0     0
#> 3:     3     0     0     1     0     0     0
#> 4:     4     0     0     0     1     0     0
#> 5:     5     0     0     0     0     1     0
#> 6:     6     0     0     0     0     0     1
enw_one_hot_encode_feature(metaobs, "week", contrasts = TRUE)
#>     week week2 week3 week4 week5 week6
#>    <int> <num> <num> <num> <num> <num>
#> 1:     1     0     0     0     0     0
#> 2:     2     1     0     0     0     0
#> 3:     3     0     1     0     0     0
#> 4:     4     0     0     1     0     0
#> 5:     5     0     0     0     1     0
#> 6:     6     0     0     0     0     1