Standardizes gender-related values into consistent numeric codes or factors.
This function maps various gender-related character strings (e.g., "male",
"female", "man", "woman", "1", "2", etc.) to standardized numeric values:
1
for male, 2
for female, and 0
for total. It supports both Chinese
and English labels. Optionally, the result can be returned as a factor with
appropriate labels.
tidy_sex(x, lang = "cn", as_factor = FALSE)
A numeric vector or a factor representing gender:
Total
Male
Female
If as_factor = TRUE
, a factor is returned with labels in the specified
language (lang
).
gender <- c("male", "men", "women", "female", "women", "man", "1", "2")
tidy_sex(gender)
#> [1] 1 1 2 2 2 1 1 2