Learn R Programming

labelled (version 2.16.0)

dictionary_to_variable_labels: Convert a dictionary data frame into a list of variable / value labels

Description

These helpers could be used to generate, from a data frame corresponding to a variable dictionary, a list of variable or value labels that could be passed to set_variable_labels() or to set_value_labels().

Usage

dictionary_to_variable_labels(dictionary, names_from = 1, labels_from = 2)

dictionary_to_value_labels( dictionary, names_from = 1, values_from = 2, labels_from = 3, delim_entries = NULL, delim_value_label = NULL, data = NULL )

Arguments

dictionary

A data frame or a tibble containing the definitions of the variable / value labels.

names_from

<tidy-select> Column containing the names of the variables.

labels_from

<tidy-select> Column containing the labels (unused if delim_value_label is provided).

values_from

<tidy-select> Column containing the values or the pairs of values and labels (see examples).

delim_entries

Optional string giving the delimiter between several pairs of value and labels if provided in a unique row. NULL if each pair is defined in a separate row (see examples).

delim_value_label

Optional string giving the delimiter between the value and the label.

data

A data frame or a tibble containing the raw data to identify the type (integer, numeric, character) of each variable.

Details

It should be noted that value labels could be

Examples

Run this code
dic <- dplyr::tibble(
  variable = c("mpg", "vs", "am", "cyl"),
  label = c("miles / gallon", "Engine", "Transmission", NA),
  values = c(NA, "0:V-Shaped,1:straight", "0:automatic,1:manual", NA)
)
dic

l <- dic %>% dictionary_to_variable_labels()
l
mtcars %>%
  set_variable_labels(.labels = l) %>%
  look_for()

vl <- dic %>%
  dictionary_to_value_labels(
    values_from = values,
    delim_entries = ",",
    delim_value_label = ":",
    data = mtcars
  )
mtcars %>%
  set_value_labels(.labels = vl) %>%
  look_for()

dic2 <- dplyr::tibble(
  variable = c("am", "am", "vs", "vs"),
  labels = c("0:automatic", "1:manual", "0:V-shaped", "1:straight")
)
dic2 %>%
  dictionary_to_value_labels(
    delim_value_label = ":",
    data = mtcars
  )

dic3 <- dplyr::tibble(
  code = c(0, 1, 0, 1),
  label = c("automatic", "manual", "V-shaped", "straight"),
  var = c("am", "am", "vs", "vs")
)
dic3 %>%
  dictionary_to_value_labels(
    names_from = var,
    values_from = code,
    labels_from = label,
    data = mtcars
  )

Run the code above in your browser using DataLab