Search code examples
rspssr-haven

Dynamically create value labels with haven::labelled


I am using haven::labelled to set value labels of a variable. The goal is to create a fully documented dataset I can export to SPSS.

Now, say I have a df value_labels of values and their value labels. I also have i df df_data with variables to which I want allocate value labels.

value_labels <- tibble(
  value = 1:6,
  labels = paste0("value", 1:6)
)

df_data <- tibble(
  id = 1:10, 
  var = floor(runif(10, 1, 6))
)

Manually, I would create value labels for df_data$var like so:

df_data$var <- haven::labelled(df_data$var, labels = c(values1 = 1, values2 =  2, values3 = 3, values4 = 4, values5 = 5, values6 = 6))

But since I have more than 16 datasets with close to 7 000 columns I need a more dynamic way of assigning value labels. Note that there is, as i understand it, difference between "values1" = 1 and values1 = 1 (quotations marks), depending on the variable class.

Note that I use haven::labelled since it is the only way, so far, I have been able to successfully export a .sav-file with value labels. I have tried sjlabelled, but with no luck.


Solution

  • We can deframe the 'value_labels', use that as labels argument in the labelled function

    library(dplyr)
    library(tibble)
    df_data %>%
         mutate(var = haven::labelled(var, labels = deframe(value_labels[2:1])))
    

    If there are more columns, then use mutate_at

    df_data %>%
         mutate_at(vars(var), ~ haven::labelled(., labels = deframe(value_labels[2:1])))
    

    With multiple datasets, place them in a list and use map to loop over the list and apply at once

    library(purrr)
    keyval <- deframe(value_labels[2:1])
    list(df_data, df_data) %>%
        map( ~
              .x %>%
                 mutate_at(vars(var), ~ haven::labelled(., labels = 
                 keyval)))