
Recode a categorical column by explicit name-to-name mapping
Source:R/categorical_guard.R
morie_safe_recode.RdThe only recode rmorie endorses: every mapping is written
old_label = new_label by NAME, positional and index-based
recoding are impossible, and any value not covered by the mapping
is an ERROR (never a silent NA or pass-through) unless
explicitly listed in keep.
Usage
morie_safe_recode(x, mapping, keep = character())Value
A character vector (with an morie_recode_audit
attribute recording the applied mapping) — convert with
morie_safe_factor to fix levels explicitly.
See also
morie_crosstab_verify to prove the recode.
Examples
x <- c("W", "B", "O", "W")
morie_safe_recode(x, c(W = "White", B = "Black", O = "Other"))
#> [1] "White" "Black" "Other" "White"
#> attr(,"morie_recode_audit")
#> attr(,"morie_recode_audit")$mapping
#> W B O
#> "White" "Black" "Other"
#>
#> attr(,"morie_recode_audit")$kept
#> character(0)
#>
#> attr(,"morie_recode_audit")$checksum
#> [1] "992b6c4a35a3cea9e5875c90f562a3259fe9f08f1eb2fac9754d7c5b5f52bdd3"
#>