Learn R Programming

sjmisc (version 1.5)

split_var: Split numeric variables into smaller groups

Description

Recode numeric variables into equal sized groups, i.e. a variable is cut into a smaller number of groups at specific cut points.

Usage

split_var(x, groupcount, as.num = FALSE, val.labels = NULL,
  var.label = NULL, inclusive = FALSE)

Arguments

x
Numeric vector, data frame or list of numeric vectors, which should split into groups.
groupcount
The new number of groups that x should be split into.
as.num
Logical; if TRUE, the recoded variable will be returned as numeric vector. If FALSE, a factor is returned.
val.labels
Optional character vector, to set value label attributes of recoded variable (see set_labels). If NULL (default), no value labels will be set.
var.label
Optional string, to set variable label attribute for the recoded variable (see set_label). If NULL (default), variable label attribute of x will be used (if present).
inclusive
Logical; if TRUE, cut point value are included in the preceeding group. This may be necessary if cutting a vector into groups does not define proper ("equal sized") group sizes. See 'Note' and 'Examples'.

Value

  • A grouped variable with equal sized groups.

Details

split_var splits a variable into equal sized groups, where the amount of groups depends on the groupcount-argument. Thus, this functions cuts a variable into groups at the specified quantiles. By contrast, group_var recodes a variable into groups, where all values within a group have the same range (e.g., from 1-5, 6-10, 11-15 etc.).

See Also

Examples

Run this code
data(efc)
# non-grouped
table(efc$neg_c_7)

# split into 3 groups
table(split_var(efc$neg_c_7, 3))


# original
table(efc$e42dep)

# two groups, non-inclusive cut-point
# vector split leads to unequal group sizes
table(split_var(efc$e42dep, 2))

# two groups, inclusive cut-point
# group sizes are equal
table(split_var(efc$e42dep, 2, inclusive = TRUE))

Run the code above in your browser using DataLab