Learn R Programming

sjmisc (version 1.0.2)

group_var: Recode count variables into grouped factors

Description

Recode count variables into grouped factors.

Usage

group_var(var, groupsize = 5, asNumeric = TRUE, rightInterval = FALSE,
  autoGroupCount = 30)

Arguments

var
The count variable, which should recoded into groups.
groupsize
The group-size, i.e. the range for grouping. By default, for each 5 categories a new group is defined, i.e. groupsize=5. Use groupsize="auto" to automatically resize a variable into a maximum of 30 groups (which is the ggplot-def
asNumeric
If TRUE (default), the recoded variable will be returned as numeric vector. If FALSE, a factor is returned.
rightInterval
If TRUE, grouping starts with the lower bound of groupsize. In this case, groups cover the ranges from 50-54, 55-59, 60-64 etc. If FALSE (default), grouping starts with the upper bound of groupsize. In
autoGroupCount
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 parameter will be ignored.

Value

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

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 autoGroupCount 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).

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)
library(sjPlot)
sjp.frq(efc$e17age,
        title = get_var_labels(efc$e17age),
        type = "h",
        showValueLabels = FALSE)

# bar plot with EUROFAMCARE sample dataset
# grouped variable
data(efc)
ageGrp <- group_var(efc$e17age)
ageGrpLab <- group_labels(efc$e17age)
library(sjPlot)
sjp.frq(ageGrp,
        title = get_var_labels(efc$e17age),
        axisLabels.x = ageGrpLab)

Run the code above in your browser using DataLab