Learn R Programming

sjmisc (version 2.2.0)

to_dummy: Split (categorical) vectors into dummy variables

Description

This function splits categorical or numeric vectors with more than two categories into 0/1-coded dummy variables.

Usage

to_dummy(x, ..., var.name = "name", suffix = c("numeric", "label"))

Arguments

x
A vector or a data frame.
...
Optional, unquoted names of variables. Required, if either x is a data frame and no vector, or if only selected variables from x should be used in the function. See 'Examples'.
var.name
Indicates how the new dummy variables are named. Use "name" to use the variable name or any other string that will be used as is. Only applies, if x is a vector. See 'Examples'.
suffix
Indicates which suffix will be added to each dummy variable. Use "numeric" to number dummy variables, e.g. x_1, x_2, x_3 etc. Use "label" to add value label, e.g. x_low, x_mid, x_high. May be abbreviated.

Value

A data frame with dummy variables for each category of x, or data where new dummy variables are appended as additional columns. The dummy coded variables are of type atomic.

Examples

Run this code
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(c172code, e42dep)


Run the code above in your browser using DataLab