Apply variable labels and value labels to a data frame using a configuration object.
Arguments
- df
A data frame.
- config
A configuration object or path to YAML file containing:
- label_lookup
Named list of variable labels.
- value_map
Named list of value-label mappings. Each mapping must contain columns
num_valueandchr_value.
- strict
Logical. If
TRUE, variables containing values not present invalue_map$num_valuewill trigger an error. IfFALSE, invalid values are converted toNA.
Details
Variable labels are retrieved from config$label_lookup.
Value labels are retrieved from config$value_map.
Variables without a value map will still receive a variable label if
one is defined in config$label_lookup.
When strict = TRUE, variables containing values not defined in
the corresponding value map will trigger an error.
When strict = FALSE, values not defined in the value map are
converted to NA and processing continues.
Examples
cfg <- structure(list(
label_lookup = list(
kjonn = "IOs kjønn"
),
value_map = list(
kjonn = data.frame(
num_value = c(1L, 2L),
chr_value = c("Mann", "Kvinne")
)
)
), class = "cc_config")
df <- data.frame(
kjonn = c("1", "2", "D")
)
apply_labels(df, cfg, strict = FALSE)
#> Warning: Variable 'kjonn': 1 value(s) converted to NA.
#> kjonn
#> 1 1
#> 2 2
#> 3 NA
