Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R to Stata: Exporting dataframe with variable labels

I have a dataset in R to which I applied variable labels using the {Hmisc} package. However, when I export the dataset to Stata (using the write.dta function in the {foreign} package), the variable labels do not appear in Stata. Rather, the variable names appear also as variable labels. The dataset contains variables like this:

X1 X2 X3

In Stata, I would like for the variables to have variable labels associated with the variable name as such:

X1 "State" X2 "PerCapitaIncome" X3 "Population"

Of course, this would all be easier if I could just apply labels in Stata rather than R, but I'm trying to provide code to a researcher who uses R exclusively. Unfortunately, I need to send the data to a data repository, which requires the dataset file format to be in Stata.

I tried to modify the code provided here: information from `label attribute` in R to `VARIABLE LABELS` in SPSS. It didn't work.

This is how I generated variable labels:

library(Hmisc)
label(data[,1]) <- "State"
label(data[,2]) <- "Per Capita Income"
label(data[,3]) <- "Population"

To export to Stata, I used this:

library(foreign)
write.dta(data,file="C:/Users/Me/Desktop/data.dta")

Based on the other post, I tried this to make the variable labels "stick":

df<-data
get.var.labels <- function(data){
a<-do.call(llist,data)
tempout<-vector("list",length(a))
for (i in 1:length(a)){
tempout[[i]]<-label(a[[i]])
}
b<-unlist(tempout)
structure(c(b),.Names=names(data))
}
attributes(df)$variable.labels=get.var.labels(df)

That code was written to export to SPSS, so I didn't expect it to work. Still, I'm hoping that I might find something similar to do the same for Stata.

Any help would be greatly appreciated!!

By the way, the data frame does have column names, but I wanted them to be more descriptive for the purposes of data management. At the same time, I want to retain the original column names (which are basically X1, X2, X3) so the researcher can continue referencing the variables that way.

like image 527
user2394406 Avatar asked May 17 '13 15:05

user2394406


1 Answers

You can use the function

write.dta

from the package

foreign

to deal with that issue. But first, you have to add an attribute to your dataset like this:

attr(data, "var.labels") <- c("State", "Per Capita Income","Population")

then export your dataset:

write.dta(data, file="mydata.dta")

That is it! It works for me, I do hope it'll be the case for you.

like image 152
AnatoleG Avatar answered Sep 25 '22 09:09

AnatoleG