Skip to contents

Convert data.table's factor to one-hot encoding in-place

Usage

dt_set_one_hot(x, xname = NULL, verbosity = 1L)

Arguments

x

data.table: Input data.table. Will be modified in-place.

xname

Character, optional: Dataset name.

verbosity

Integer: Verbosity level.

Value

The input, invisibly, after it has been modified in-place.

Author

EDG

Examples

ir <- data.table::as.data.table(iris)
# dt_set_one_hot operates ***in-place***; therefore no assignment is used:
dt_set_one_hot(ir)
#> 2026-03-15 07:49:56 
#> One hot encoding Species...
#>  [dt_set_one_hot]
#> 2026-03-15 07:49:56 
#> Done
#>  [dt_set_one_hot]
ir
#>      Sepal.Length Sepal.Width Petal.Length Petal.Width Species_setosa
#>             <num>       <num>        <num>       <num>          <num>
#>   1:          5.1         3.5          1.4         0.2              1
#>   2:          4.9         3.0          1.4         0.2              1
#>   3:          4.7         3.2          1.3         0.2              1
#>   4:          4.6         3.1          1.5         0.2              1
#>   5:          5.0         3.6          1.4         0.2              1
#>  ---                                                                 
#> 146:          6.7         3.0          5.2         2.3              0
#> 147:          6.3         2.5          5.0         1.9              0
#> 148:          6.5         3.0          5.2         2.0              0
#> 149:          6.2         3.4          5.4         2.3              0
#> 150:          5.9         3.0          5.1         1.8              0
#>      Species_versicolor Species_virginica
#>                   <num>             <num>
#>   1:                  0                 0
#>   2:                  0                 0
#>   3:                  0                 0
#>   4:                  0                 0
#>   5:                  0                 0
#>  ---                                     
#> 146:                  0                 1
#> 147:                  0                 1
#> 148:                  0                 1
#> 149:                  0                 1
#> 150:                  0                 1