Dynamically create value labels with haven::labelled

可紊 提交于 2020-01-24 23:42:07

问题


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.


回答1:


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)))


来源:https://stackoverflow.com/questions/59360232/dynamically-create-value-labels-with-havenlabelled

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!