format_age_custom: Create consistent, complete customized age groups

Description Usage Arguments Details Value See Also Examples

View source: R/format_age.R

Description

Given a vector of age group labels, create a factor that contains levels for all age groups, as defined by the breaks argument. The labels may include an open age group, ie an age group with no upper limit.

Usage

1
format_age_custom(x, breaks = NULL, open_last = TRUE)

Arguments

x

A vector of age group labels.

breaks

A vector of strictly increasing integer values.

open_last

Whether the final age group has no upper limit. Defaults to TRUE.

Details

format_age_custom is the most flexible of the format_age functions in that the age groups can have any combination of widths.

breaks is used to specify the points at which each age group starts and finishes, and open_last is used to specify whether the final age group has an upper limit. If breaks has length n, then the final age group is defined as follows:

open_last Final age group
TRUE [breaks[n], Inf)
FALSE [breaks[n-1], breaks[n])

If x contains NA, then the levels of the factor created by format_age_custom also contain NA.

Value

A factor with length equal to x.

See Also

Other functions for creating age groups are

date_to_age_year calculates ages from dates.

Examples

1
2
3
4
5
6
7
format_age_custom(x = c(22, 11, 85),
                  breaks = c(0, 15, 45, 70))
format_age_custom(x = c("90+", "19-40", "22", NA),
                  breaks = c(0, 15, 60))
format_age_custom(x = c("50-59", "19-40", "31"),
                  breaks = c(15, 45, 60),
                  open_last = FALSE)

johnrbryant/demprep documentation built on Dec. 31, 2021, 11:58 a.m.