sex_from_chi: Extract sex from the CHI number

View source: R/sex_from_chi.R

sex_from_chiR Documentation

Extract sex from the CHI number

Description

sex_from_chi takes a CHI number or a vector of CHI numbers and returns the sex as implied by the CHI number(s). The default return type is an integer but this can be modified.

Usage

sex_from_chi(
  chi_number,
  male_value = 1L,
  female_value = 2L,
  as_factor = FALSE,
  chi_check = TRUE
)

Arguments

chi_number

a CHI number or a vector of CHI numbers with character class.

male_value, female_value

optionally supply custom values for Male and Female. Note that that these must be of the same class.

as_factor

logical, optionally return as a factor with labels 'Male' and 'Female'. Note that this will override any custom values supplied with male_value or female_value.

chi_check

logical, optionally skip checking the CHI for validity which will be faster but should only be used if you have previously checked the CHI(s).

Details

The Community Health Index (CHI) is a register of all patients in NHS Scotland. A CHI number is a unique, ten-digit identifier assigned to each patient on the index.

The ninth digit of a CHI number identifies a patient's sex: odd for men, even for women.

The default behaviour for sex_from_chi is to first check the CHI number is valid using check_chi and then to return 1 for male and 2 for female.

There are options to return custom values e.g. 'M' and 'F' or to return a factor which will have labels 'Male' and ⁠'Female')⁠

Value

a vector with the same class as male_value and female_value, (integer by default) unless as_factor is TRUE in which case a factor will be returned.

Examples

sex_from_chi("0101011237")
sex_from_chi(c("0101011237", "0101336489", NA))
sex_from_chi(
  c("0101011237", "0101336489", NA),
  male_value = "M",
  female_value = "F"
)
sex_from_chi(c("0101011237", "0101336489", NA), as_factor = TRUE)

library(dplyr)
df <- tibble(chi = c("0101011237", "0101336489", NA))
df %>% mutate(chi_sex = sex_from_chi(chi))

Health-SocialCare-Scotland/phsmethods documentation built on Jan. 28, 2024, 3:18 a.m.