View source: R/getCensusData.R
| aggregateByAgeGroups | R Documentation |
Aggregate per-age population counts into coarser age groups defined by the
(sorted) lower bounds in age_groups. When length(age_groups) == 1, all
ages >= that value are aggregated into a single open-ended group ("Xplus").
When length(age_groups) > 1, groups are formed as
age_groups[i] to age_groups[i+1] - 1 for i = 1:(n-1) and
age_groups[n] and above for the final group. Human-readable labels are produced:
"under1" for the 0–0 group, "ageX" for single-year groups, "XtoY" for ranges,
and "Xplus" for the final open group. When verbose = TRUE, the function prints
aggregation summaries to the console for each group using cat().
aggregateByAgeGroups(ages, pops, age_groups, verbose = FALSE)
ages |
Numeric vector of ages (typically integers) corresponding to the
entries in |
pops |
Numeric vector of population counts for each age; must be the
same length as |
age_groups |
Numeric vector of lower bounds for desired age groups. Must be sorted in ascending order. If length is 1, the single value defines an "Xplus" group (ages >= X). For length > 1, contiguous non-overlapping groups are created as described above. |
verbose |
Logical, if TRUE prints aggregation messages for each group. Default is FALSE. |
Group boundaries are inclusive at both ends for finite ranges (i.e. ages satisfying lower <= age <= upper). For the last group the upper bound is infinite.
If no ages fall into a group the aggregated count for that group is 0
(because sum(numeric(0)) == 0).
When verbose = TRUE, the function writes progress messages to the console
with cat() for each aggregated group (useful for debugging / logging).
By default (verbose = FALSE), the function is silent.
A named list with components:
Numeric vector of aggregated population counts, one element per group.
Character vector of labels for each group (e.g. "under1", "age5", "0to4", "65plus").
List of numeric vectors of length 2 giving the inclusive lower and upper bounds for each group; the upper bound for the final group is Inf.
# Multiple groups example
ages <- 0:100
pops <- rep(100, length(ages))
aggregateByAgeGroups(ages, pops, c(0, 5, 18, 65))
# Single open-ended group (65plus)
aggregateByAgeGroups(ages, pops, 65)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.