Learn R Programming

sjmisc (version 1.2)

dicho: Dichotomize variables

Description

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

Usage

dicho(x, dich.by = "median", dich.val = -1, as.num = FALSE,
  var.label = NULL, val.labels = NULL, dichBy, dichVal, asNum)

Arguments

x
Variable (vector), data.frame or list of variables that should be dichotomized
dich.by
Indicates the split criterion where a variable is dichotomized [object Object],[object Object],[object Object]
dich.val
Numeric, indicates a value where x is dichotomized when dich.by = "value". Note that dich.val is inclusive, i.e. dich.val = 10 will split x into one group with values from l
as.num
Logical, if TRUE, return value will be numeric, not a factor.
var.label
Optional string, to set variable label attribute for the dichotomized variable (see set_label). If NULL (default), variable label attribute of x will be used (if present).
val.labels
Optional character vector (of length two), to set value label attributes of dichotomized variable (see set_labels). If NULL (default), no value labels will be set.
dichBy
deprecated; use dich.by instead
dichVal
deprecated; use dich.val instead
asNum
deprecated; use as.num instead

Value

  • A dichotomized factor (or numeric, if as.num = TRUE) variable (0/1-coded), respectively a data frame or list 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))

# dichtomize several variables in a list
dummy <- list(efc$c12hour, efc$e17age, efc$c160age)
dicho(dummy)

# dichotomize and set labels. requires package
# sjPlot to test
library(sjPlot)
sjp.frq(dicho(efc$e42dep,
              var.label = "Dependency (dichotomized)",
              val.labels = c("lower", "higher")))

Run the code above in your browser using DataLab