One hot encoding of data.frames

onehot(dataset, all = FALSE, df = FALSE)

Arguments

dataset

Dataset to endcode

all

Extract all factor levels (e.g., for tree-based models)

df

Return a data.frame (tibble)

Examples

head(onehot(diamonds, df = TRUE))
#> (Intercept) price carat claritySI2 claritySI1 clarityVS2 clarityVS1 #> 7718 1 580 0.32 0 0 0 1 #> 27822 1 650 0.34 0 1 0 0 #> 23018 1 630 0.30 0 0 1 0 #> 29608 1 706 0.35 0 0 0 0 #> 39543 1 1080 0.40 0 0 1 0 #> 1919 1 3082 0.60 0 0 0 0 #> clarityVVS2 clarityVVS1 clarityIF cutGood cutVery Good cutPremium #> 7718 0 0 0 0 0 0 #> 27822 0 0 0 0 1 0 #> 23018 0 0 0 0 1 0 #> 29608 1 0 0 0 0 0 #> 39543 0 0 0 0 0 1 #> 1919 0 1 0 0 0 0 #> cutIdeal colorE colorF colorG colorH colorI colorJ depth table x y #> 7718 1 0 0 0 1 0 0 61.0 56.0 4.43 4.45 #> 27822 0 0 0 1 0 0 0 63.4 57.0 4.45 4.42 #> 23018 0 0 0 1 0 0 0 63.1 58.0 4.27 4.23 #> 29608 1 0 0 0 1 0 0 59.2 56.0 4.60 4.65 #> 39543 0 0 1 0 0 0 0 62.6 58.0 4.72 4.68 #> 1919 1 1 0 0 0 0 0 62.5 53.7 5.35 5.43 #> z date #> 7718 2.71 15396 #> 27822 2.81 15396 #> 23018 2.68 15396 #> 29608 2.74 15396 #> 39543 2.94 15396 #> 1919 3.38 15396
head(onehot(diamonds, all = TRUE, df = TRUE))
#> price carat clarityI1 claritySI2 claritySI1 clarityVS2 clarityVS1 #> 7718 580 0.32 0 0 0 0 1 #> 27822 650 0.34 0 0 1 0 0 #> 23018 630 0.30 0 0 0 1 0 #> 29608 706 0.35 0 0 0 0 0 #> 39543 1080 0.40 0 0 0 1 0 #> 1919 3082 0.60 0 0 0 0 0 #> clarityVVS2 clarityVVS1 clarityIF cutFair cutGood cutVery Good cutPremium #> 7718 0 0 0 0 0 0 0 #> 27822 0 0 0 0 0 1 0 #> 23018 0 0 0 0 0 1 0 #> 29608 1 0 0 0 0 0 0 #> 39543 0 0 0 0 0 0 1 #> 1919 0 1 0 0 0 0 0 #> cutIdeal colorD colorE colorF colorG colorH colorI colorJ depth table #> 7718 1 0 0 0 0 1 0 0 61.0 56.0 #> 27822 0 0 0 0 1 0 0 0 63.4 57.0 #> 23018 0 0 0 0 1 0 0 0 63.1 58.0 #> 29608 1 0 0 0 0 1 0 0 59.2 56.0 #> 39543 0 0 0 1 0 0 0 0 62.6 58.0 #> 1919 1 0 1 0 0 0 0 0 62.5 53.7 #> x y z date #> 7718 4.43 4.45 2.71 15396 #> 27822 4.45 4.42 2.81 15396 #> 23018 4.27 4.23 2.68 15396 #> 29608 4.60 4.65 2.74 15396 #> 39543 4.72 4.68 2.94 15396 #> 1919 5.35 5.43 3.38 15396