Learn R Programming

sjmisc (version 1.0.2)

dicho: Dichotomize variables

Description

Dichotomizes variables into dummy variables (0/1). Dichotomization is either done by median, mean or a specific value (see dichBy). Either single vectors or complete data frames can be dichotomized.

Usage

dicho(x, dichBy = "median", dichVal = -1, asNum = FALSE)

Arguments

x
The variable (vector) or data frame that should be dichotomized.
dichBy
Indicates the split criterion where the variable is dichotomized.
  • By default,varis split into two groups at the median (dichBy = "median"ordichBy = "md").
  • dichBy = "mean"(ordichBy =
dichVal
numeric, indicates a value where var is dichotomized when dichBy = "value". Note that dichVal is inclusive, i.e. dichVal = 10 will split var into one group with values from lowest t
asNum
logical, if TRUE, return value will be numeric, not a factor.

Value

  • A dichotomized factor (or numeric, if asNum = TRUE) variable (0/1-coded), respectively a data frame of dichotomized factor (or numeric) variables.

Examples

Run this code
data(efc)
summary(efc$c12hour)
table(dicho(efc$c12hour))
table(dicho(efc$c12hour, "mean"))
table(dicho(efc$c12hour, "value", 30))

# sample data frame, values from 1-4
head(efc[, 6:10])
# dichtomized values (1 to 2 = 0, 3 to 4 = 1)
head(dicho(efc[, 6:10], "v", 2))

Run the code above in your browser using DataLab