One Hot Encoding of vector

onehot(x)

Arguments

x

vector with D possible categorical values (will be coerced to factor)

Value

matrix with D columns and same number of columns as length of x

Examples

onehot(1:5)
#> as.factor(x)1 as.factor(x)2 as.factor(x)3 as.factor(x)4 as.factor(x)5 #> 1 1 0 0 0 0 #> 2 0 1 0 0 0 #> 3 0 0 1 0 0 #> 4 0 0 0 1 0 #> 5 0 0 0 0 1 #> attr(,"assign") #> [1] 1 1 1 1 1 #> attr(,"contrasts") #> attr(,"contrasts")$`as.factor(x)` #> [1] "contr.treatment" #>