Learn R Programming

sjmisc (version 1.0.0)

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

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

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

Run the code above in your browser using DataLab