cleanAgeGroup: Clean up age group labels

Description Usage Arguments Details Value See Also Examples

View source: R/miscellaneous-functions.R

Description

Reformat a vector of age-group labels, so that it matches the format expected by functions such as Counts and Values.

Usage

1
2
3

Arguments

age

A vector of age labels.

Details

cleanAgeGroup strips off spaces or other symbols, converts text to numbers, and otherwise tries to guess at the age-group labels.

It is common for age groups to be coded using only the first year, so that, for instance, 0, 1, 5, ..., 100 is short for "0", "1-4", "5-9", ..., "100+". cleanAgeGroup interprets a vectorage as a set of age-group labels if the unique values from age, once sorted, form a sequence, 0, 5, 10, ... or 0, 1, 5, 10, ....

cleanAgeGroup does the reformatting. cleanAgeGroupConc constructs a {Concordance} between the old and new age-group labels.

cleanAgeGroup is suitable for interactive use, or for one-off analyses, where the data will not change in future.

Using cleanAgeGroupConc may be safer in production code where the data may change. The workflow is as follows:

  1. Use a vector of age labels to construct a concordance.

  2. Store the concordance.

  3. When new data arrives, use function translate, plus the stored concordance, to clean the age data.

The advantange of this work flow is that translate will throw an error if it strikes an age label that it does not recognize.

Value

A character vector of reformatted age labels.

See Also

ageToAgeGroup converts exact ages into age groups.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
x1 <- c("0 - 4 years", "90 plus years", "25 - 29 years")
x2 <- c("10 to 19 Yr", "80 years and over")
x3 <- c(10, 0, 5, 1, 5)
x4 <- c("5--9", "10plus", "0--4")
x5 <- c("10plus", "10plus", "5--9", "5--9")

## Use cleanAgeGroup directly
cleanAgeGroup(x1)
cleanAgeGroup(x2)
cleanAgeGroup(x3)

## Set up concordance and use that
conc <- cleanAgeGroupConc(x4)
translate(x5, concordance = conc)

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