ageToAgeGroup: Convert exact ages to age groups

Description Usage Arguments Details Value See Also Examples

View source: R/miscellaneous-functions.R

Description

Convert a vector of exact ages to a vector of age groups. The age groups are formatted in the way expected by functions such as Counts and Values.

Usage

1
ageToAgeGroup(age, breaks = seq(0, 100, 5), firstOpen = FALSE, lastOpen = TRUE)

Arguments

age

A vector of exact ages. A numeric vector, or a vector than can be coerced to numeric.

breaks

A vector of breaks, specifying the points dividing the age groups.

firstOpen

Logical. Whether the first age group is "open", i.e. has no lower bound. Defaults to FALSE.

lastOpen

Logical. Whether the last age group is "open", i.e. has no upper bound. Defaults to TRUE.

Details

‘Exact’ ages can be numbers such as 6.238 or 77.13, but are more typically integers such as 6 or 77.

If age is a factor, then ageToAgeGroup will coerce it to a character vector before trying to coerce it to numeric. See below for an example.

By default, ageToAgeGroup creates 5-year age groups. See below for examples of other groupings.

Value

A factor, the same length as age.

See Also

timeToPeriod. seq (in combination with c) is useful for creating complicated breaks arguments.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
age <- c(22, 18, 4, 0, 89, 103, 7)
## 5-year age groups, 0-4, 5-9, ..., 95-99, 100+
ageToAgeGroup(age)
## 1-year age groups, 0, 1, ..., 89, 90+
ageToAgeGroup(age, breaks = 0:90)
## age groups 0, 1-4, 5-9, 10-14, ..., 85+
ageToAgeGroup(age, breaks = c(0, 1, seq(5, 85, 5)))
## last age group closed
ageToAgeGroup(age = c(0, 17, 14, 3, 9),
              breaks = seq(0, 20, 5),
              lastOpen = FALSE)

StatisticsNZ/dembase documentation built on Dec. 25, 2021, 4:49 p.m.