Skip to contents

NCHS changed the sex coding at data year 2003: 1979-2002 use a numeric code (`1` = male, `2` = female); 2003 onward use a character code (`"M"`, `"F"`). This maps either scheme to the strings `"male"` / `"female"` (and `NA` for anything else), matching the `sex` labels used by [pop_est] so downstream population joins need no relabeling.

Usage

categorize_sex(sex_column, year = NULL)

Arguments

sex_column

a vector of raw NCHS sex codes

year

data year: a scalar, a vector aligned to `sex_column`, or `NULL` to infer the era from the column type

Value

a character vector of `"male"` / `"female"` / `NA`

Details

The era is taken from `year` when supplied (authoritative). When `year` is `NULL`, the era is inferred from the column's type – a numeric column is the pre-2003 scheme, a character column the 2003+ scheme – with a warning; pass `year` to be explicit (a character `"1"`/`"2"` re-read from a CSV would otherwise be treated as the modern scheme and map to `NA`).

Examples

categorize_sex(c(1, 2, 9), year = 2000)      # "male" "female" NA
#> [1] "male"   "female" NA      
categorize_sex(c("M", "F", "U"), year = 2019) # "male" "female" NA
#> [1] "male"   "female" NA