Learn R Programming

sjmisc (version 1.5)

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 = "numeric", data = NULL)

Arguments

x
a vector (variable).
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. 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_h
data
optional, a data frame where the new dummy variables are appended as additional columns.

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"))

Run the code above in your browser using DataLab