Skip to content

Instantly share code, notes, and snippets.

@avallecam
Last active September 21, 2018 15:55
Show Gist options
  • Save avallecam/661d9bf335ff310b4957d873efcd4523 to your computer and use it in GitHub Desktop.
Save avallecam/661d9bf335ff310b4957d873efcd4523 to your computer and use it in GitHub Desktop.
How to set variable labels from labels stored in a dataframe?
# from dataframes to variable labels?
library(tidyverse)
library(haven)
library(labelled)
set.seed(22)
new_db <- data_frame(age = floor(rnorm(6,25,10)),
sex = gl(2,1,6, labels = c("f","m")))
var.labels <- data_frame(variable = c("age","sex"),
label= c("Age in Years","Sex of the participant"))
new_db %>%
labelled::set_variable_labels(
age = var.labels$label[1],
sex = var.labels$label[2]
) %>%
haven::write_dta("data/new_db.dta")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment