Go from a named list to a key-value data frame or data dictionary and back
Source:R/metadata.R
list_to_dict.Rd
Sometimes, you'll want to have variable labels in a data.frame, sometimes
you'll have imported an existing data dictionary and will need to turn it
into a list before setting labelled::var_label()
.
Examples
data('bfi')
labels <- var_label(bfi)
head(labels, 2)
#> $A1
#> NULL
#>
#> $A2
#> NULL
#>
dict <- list_to_dict(labels)
#> Error in dplyr::left_join(tibble::tibble(variable = names(named_list)), tidyr::gather(tibble::as_tibble(purrr::compact(named_list)), "variable", "label"), by = "variable"): Join columns in `y` must be present in the data.
#> ✖ Problem with `variable`.
head(dict, 2)
#> Error: object 'dict' not found
head(dict_to_list(list_to_dict(labels)), 2)
#> Error in dplyr::left_join(tibble::tibble(variable = names(named_list)), tidyr::gather(tibble::as_tibble(purrr::compact(named_list)), "variable", "label"), by = "variable"): Join columns in `y` must be present in the data.
#> ✖ Problem with `variable`.