Transform a data frame to a numeric matrix by one-hot encoding factors. The last factor value is always omitted to prevent having a singular matrix when adding a column of 1s (intercept) in models.

covar_from_df(df)

Arguments

df

A data frame.

Value

A numeric matrix.

Examples

mat <- covar_from_df(iris) head(mat)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Speciesversicolor #> 1 5.1 3.5 1.4 0.2 0 #> 2 4.9 3.0 1.4 0.2 0 #> 3 4.7 3.2 1.3 0.2 0 #> 4 4.6 3.1 1.5 0.2 0 #> 5 5.0 3.6 1.4 0.2 0 #> 6 5.4 3.9 1.7 0.4 0 #> Speciesvirginica #> 1 0 #> 2 0 #> 3 0 #> 4 0 #> 5 0 #> 6 0