R to Stata: Exporting dataframe with variable labels
Asked Answered
S

2

7

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.

Sassenach answered 17/5, 2013 at 15:44 Comment(2)
Sympathies, but this is in essence "code I am not showing you does not work". It would help to see a simple reproducible example of what you have in R, exactly how you exported it, and what it looks like in Stata. Data plus code, please.Peachy
As above - show us what "I applied variable labels using the Hmisc package" means. Was this a data frame? Doesn't it have column names? Try with a test data frame or something. Otherwise we're stuck.Nereus
P
3

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.

Pokey answered 25/2, 2015 at 10:56 Comment(0)
A
1

From the write.dta help page: "If the "val.labels" attribute contains a string vector with a string label for each variable then this is written as the variable labels. Otherwise the variable names are repeated as variable labels." So I would just try:

write.dta(data,file="C:/Users/Me/Desktop/data.dta", 
             val.labels=c( "State", "Per Capita Income", "Population") )
Arthur answered 27/11, 2013 at 23:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.