Description Usage Arguments Value Examples
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.
1 2 | age_to_band(x, width = 5, style = "bracket", group_below = NULL,
group_above = NULL)
|
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. |
group_below, group_above |
An integer specifying end group thresholds. Must be a
multiple of |
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.
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")
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.