
Decode a labelled import by its value labels, by code
Source:R/categorical_guard.R
morie_decode_labelled.RdVectors read from SPSS, Stata or SAS carry their categories as numeric
codes with a labels attribute (label = code). The categories are the
labels looked up BY CODE; the codes themselves, their order, and the
alphabetical order of the labels are all irrelevant, and treating any of
them as the category is the documented failure. This function looks each
code up in the attribute and refuses codes that have no label.
Value
A factor whose levels are the labels in CODE order (not
alphabetical), with a morie_recode_audit attribute.
Examples
x <- structure(c(1, 2, 2, 4), labels = c(White = 1, Black = 2, Other = 3, Unknown = 4))
morie_decode_labelled(x)
#> [1] White Black Black Unknown
#> attr(,"morie_recode_audit")
#> attr(,"morie_recode_audit")$mapping
#> 1 2 3 4
#> White Black Other Unknown
#>
#> attr(,"morie_recode_audit")$kept
#> character(0)
#>
#> attr(,"morie_recode_audit")$checksum
#> [1] 3d394c3d6f779a313f8fbeecaf5bc4f104738096c07f03c48ab68d5eb52e8de4
#>
#> Levels: White Black Other Unknown