reformat_age {poputils} | R Documentation |
Reformat Age Group Labels
Description
Convert age group labels to one of three formats:
Single-year age groups, eg
"0"
,"1"
, ...,"99"
,"100+"
.Life table age groups, eg
"0"
,"1-4",
"5-9", ...,
"95-99",
"100+"'.Five-year age groups, eg
"0-4"
,"5-9"
, ...,"95-99"
,"100+"
.
By default reformat_age()
returns a factor
that includes all intermediate age groups.
See below for examples.
Usage
reformat_age(x, factor = TRUE)
Arguments
x |
A vector. |
factor |
Whether the return value should be a factor. |
Details
reformat_age()
applies the following algorithm:
Tidy and translate text, eg convert
"20 to 24 years"
to"20-24"
, convert"infant"
to"0"
, or convert"100 or more"
to"100+"
.Check whether the resulting labels could have been produced by
age_labels()
. If not, throw an error.If
factor
isTRUE
(the default), then return a factor. The levels of this factor include all intermediate age groups. Otherwise return a character vector.
When x
consists entirely of numbers, reformat_age()
also checks for two special cases:
If every element of
x
is a multiple of 5, and ifmax(x) >= 50
, thenx
is assumed to describe 5-year age groupsIf every element of
x
is 0, 1, or a multiple of 5, withmax(x) >= 50
, thenx
is assumed to describe life table age groups.
Value
If factor
is TRUE
,
then reformat_age()
returns a factor;
otherwise it returns a character vector.
See Also
Examples
reformat_age(c("80 to 84", "90 or more", "85 to 89"))
## factor contains intermediate level missing from 'x'
reformat_age(c("80 to 84", "90 or more"))
## non-factor
reformat_age(c("80 to 84", "90 or more"),
factor = FALSE)
## single
reformat_age(c("80", "90plus"))
## life table
reformat_age(c("0",
"30-34",
"10--14",
"1-4 years"))