Last active
July 6, 2020 19:21
-
-
Save avallecam/52d3f019ed9ba498dfcda1774a3777c1 to your computer and use it in GitHub Desktop.
How to export R dataframes with character vectors to Stata?
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
if(!require("tidyverse")) install.packages("tidyverse") | |
if(!require("haven")) install.packages("haven") | |
if(!require("labelled")) install.packages("labelled") | |
library(tidyverse) | |
library(haven) | |
library(labelled) | |
#crear data frame | |
set.seed(22) | |
data_frame(age = floor(rnorm(6,25,10)), | |
sex = gl(2,1,6, labels = c("f","m")), | |
edu = rep(c("primaria","secundaria"),each=3)) %>% | |
#transformar vectores chr a fct | |
mutate_if(is.character,as.factor) %>% | |
#asignar etiquetas a variables | |
labelled::set_variable_labels( | |
age = "Edad (años)", | |
sex = "Sexo de participantes", | |
edu = "Nivel educativo") %>% | |
#grabar en dta | |
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