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.