3

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.

oskjerv
  • 198
  • 7

1 Answers1

2

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)))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Now say that I extend the df `value_labels`, so that it contains the value labels of `var1` and `var2`. `value_labels <- tibble(value = c(seq(1:6), seq(1:3)), labels = c(paste0("value", 1:6),paste0("value", 1:3)), name = c(rep("var1", 6), rep("var2", 3))))` `df_data <- tibble(id = 1:10, var1 = floor(runif(10, 1, 7)), var2 = floor(runif(10, 1, 4)))` How would you assign the correct value labels to the correct variable in `df_data`? – oskjerv Dec 17 '19 at 11:44
  • @oskjerv Can you please post as a new question. – akrun Dec 17 '19 at 14:01
  • https://stackoverflow.com/questions/59376487/dynamically-create-value-labels-with-havenlabelled-follow-up – oskjerv Dec 17 '19 at 14:36