age_to_band: Convert ages to age-bands

Description Usage Arguments Value Examples

Description

Use age_to_band() to bin ages (measured in years) into regularly spaced intervals with options for grouping values above or below given thresholds. Intervals are closed on the right and open on the left.

Usage

1
2
age_to_band(x, width = 5, style = "bracket", group_below = NULL,
  group_above = NULL)

Arguments

x

A numeric vector of non-negative values representing ages.

width

An integer specifying the size of the interval for binning. Must be greater than or equal to 2 for meaningful age bands.

style

A string specifying the format of labels assigned to the bins. style = "bracket" provides mathematical interval notation, e.g. "[5, 10)", whereas style = "hyphen" provides integer limits separated by a hyphen, e.g. "5-9". While the hyphen-style format is less precise mathematically, it may be more intuitive for audiences without a mathematical background.

group_below, group_above

An integer specifying end group thresholds. Must be a multiple of width. This is sometimes helpful when younger or older ages should be collected into wider intervals groups groups. E.g. width = 5 and group_below = 15 puts every age below 15 into a wider bin of size [0, 15) then 5-year bands proceed from there: [15, 20), [20, 25), [25, 30), etc..

Value

An ordered factor with labels determined by style. Bands that don't appear in the data are still included as levels in the resulting factor.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# Generate non-negative data
x <- runif(20, 0, 100)
age_to_band(x)              # 5-year age bands (the default)
age_to_band(x, width = 20)  # 20-year age bands

# The ordered factor output ensures the bands are
# appropriately ordered and that bands not present
# in the data are still included. This might otherwise lead to
# undesireable plot results.
y <- c(runif(10, 0, 20), runif(10, 30, 50))
plot(table(age_to_band(y)))

# `style = "bracket"` and `style = "hyphen"` have
# different labels when grouping the ends
z <- c(5, 23, 70)
age_to_band(z, group_below = 15, group_above = 65, style = "bracket")
age_to_band(z, group_below = 15, group_above = 65, style = "hyphen")

liam-c-smith/secretsauce documentation built on May 16, 2019, 7:16 p.m.