format_cohort_year: Create consistent, complete single-year cohorts

Description Usage Arguments Details Value See Also Examples

View source: R/format_cohort.R

Description

Given a vector of cohort labels, create a factor with a complete set of cohorts. The labels may include an "open" cohort with no lower limit. Apart from the open cohort, all cohorts have a width of one year.

Usage

1
format_cohort_year(x, break_min = NULL, open_first = NULL)

Arguments

x

A vector of cohort labels.

break_min

An integer or NULL (the default). If an integer, it is the year in which the oldest cohort begins.

open_first

Whether the oldest cohort has no lower limit.

Details

x must consist of labels for single-year cohorts, such as "2001", or open cohorts, such as "<2000". x must not contain labels for multi-year cohorts such as "2000-2005"

open_first defaults to TRUE if a value for break_min is supplied, or if any elements of x are open, and to FALSE otherwise.

If x has NAs, then the levels of the factor created by format_cohort_year also contain NA.

Value

A factor with the same length as x.

See Also

Other functions for reformatting cohort labels are

date_to_cohort_year creates single-year cohorts from dates.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
## the 'levels' contain all values from
## '2000' to '2005', even when these do not
## appear in the data
format_cohort_year(x = c(2000, 2005, NA, 2004))

## 'x' contains an open interval, so
## 'open_first' defaults to TRUE
format_cohort_year(x = c("<2000", "2005", NA, "2004"))

## specify 'break_min', which also makes
## 'open_first' default to TRUE
format_cohort_year(x = c(2000, 2005, NA, 2004),
                   break_min = 1998)

## set 'break_min' above minimum value of 'x'
format_cohort_year(x = c(2000, 2005, NA, 2004),
                   break_min = 2001)

## set 'open_first' to FALSE 
format_cohort_year(x = c(2000, 2005, NA, 2004),
                   open_first = FALSE)
format_cohort_year(x = c(2000, 2005, NA, 2004),
                   break_min = 1998,
                   open_first = FALSE)

## if no value for 'break_min' is supplied, and one or
## more of the labels refers to an open cohort,
## then the levels start with the latest open cohort
format_cohort_year(x = c("2000", "2005", "<2002",
                         "2004", "<2001"))

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