Split (categorical) vectors into dummy variables
This function splits categorical or numeric vectors with more than two categories into 0/1-coded dummy variables.
to_dummy(x, ..., var.name = "name", suffix = c("numeric", "label"))
x |
A vector or data frame. |
... |
Optional, unquoted names of variables that should be selected for
further processing. Required, if |
var.name |
Indicates how the new dummy variables are named. Use
|
suffix |
Indicates which suffix will be added to each dummy variable.
Use |
A data frame with dummy variables for each category of x
.
The dummy coded variables are of type atomic
.
NA
values will be copied from x
, so each dummy variable
has the same amount of NA
's at the same position as x
.
data(efc) head(to_dummy(efc$e42dep)) # add value label as suffix to new variable name head(to_dummy(efc$e42dep, suffix = "label")) # use "dummy" as new variable name head(to_dummy(efc$e42dep, var.name = "dummy")) # create multiple dummies, append to data frame to_dummy(efc, c172code, e42dep) # pipe-workflow library(dplyr) efc %>% select(e42dep, e16sex, c172code) %>% to_dummy()
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.