Learn R Programming

sjmisc (version 1.5)

group_var: Recode numeric variables into equal-ranged groups

Description

Recode numeric variables into equal spaced grouped factors, i.e. a variable is cut into a smaller number of groups, where each group has values of equal range.

Usage

group_var(var, groupsize = 5, as.num = TRUE, right.interval = FALSE,
  groupcount = 30)

Arguments

var
Numeric; variable, which should recoded into groups.
groupsize
Numeric; group-size, i.e. the range for grouping. By default, for each 5 categories of var a new group is defined, i.e. groupsize=5. Use groupsize = "auto" to automatically resize a variable into a maximum of 30 grou
as.num
Logical; if TRUE, the recoded variable will be returned as numeric vector. If FALSE, a factor is returned.
right.interval
Logical; if TRUE, grouping starts with the lower bound of groupsize. See 'Details'.
groupcount
Sets the maximum number of groups that are defined when auto-grouping is on (groupsize="auto"). Default is 30. If groupsize is not set to "auto", this argument will be ignored.

Value

  • A grouped variable, either as numeric or as factor (see paramter as.num).

Details

If groupsize is set to a specific value, the variable is recoded into several groups, where each group has a maximum range of groupsize. Hence, the amount of groups differ depending on the range of var. If groupsize = "auto", the variable is recoded into a maximum of groupcount groups. Hence, independent from the range of var, always the same amount of groups are created, so the range within each group differs (depending on var's range). right.interval determins which boundary values to include when grouping is done. If TRUE, grouping starts with the lower bound of groupsize. For example, having a variable ranging from 50 to 80, groups cover the ranges from 50-54, 55-59, 60-64 etc. If FALSE (default), grouping starts with the upper bound of groupsize. In this case, groups cover the ranges from 46-50, 51-55, 56-60, 61-65 etc. Note: This will cover a range from 46-50 as first group, even if values from 46 to 49 are not present. See 'Examples' in group_labels. If you want to split a variable into a certain amount of equal sized groups (instead of having groups where values have all the same range), use the split_var function!

See Also

Examples

Run this code
age <- abs(round(rnorm(100, 65, 20)))
age.grp <- group_var(age, 10)
hist(age)
hist(age.grp)


# histogram with EUROFAMCARE sample dataset
# variable not grouped
data(efc)
hist(efc$e17age, main = get_label(efc$e17age))

# bar plot with EUROFAMCARE sample dataset
# grouped variable
ageGrp <- group_var(efc$e17age)
ageGrpLab <- group_labels(efc$e17age)
barplot(table(ageGrp),
        main = get_label(efc$e17age),
        names.arg = ageGrpLab)

Run the code above in your browser using DataLab