Last active
September 21, 2018 15:55
-
-
Save avallecam/661d9bf335ff310b4957d873efcd4523 to your computer and use it in GitHub Desktop.
How to set variable labels from labels stored in a dataframe?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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