Reputation: 1304
I am using both Stata and R in my project and I would like to label variables in R so that Stata reads them correctly.
Take the following database with 12 variables correctly labelled in Stata
* these are Stata commands
sysuse auto
save sysuseauto.dta
I can import the data into R
# import
library(haven)
library(data.table)
auto<-read_dta("sysuseauto.dta")
Do note that R maintains all variable labels as specified in Stata. I would like to create a new variable in R
# Creating a new variable
sapply(auto,class)
auto<-setDT(auto)[,n_cars_by_foreign:=.N,by=.(foreign)]
and label it so that when I open the new data in Stata, it is correctly labelled.
However, I am unable to label the variable so that Stata reads it. I have tried the following
attr(auto, "haven_labelled") <- c(n_cars_by_foreign = "Number of cars by foreign")
write_dta(auto, "sysuseauto2.dta")
# and
attr(auto, "var.labels") <- c(n_cars_by_foreign = "Number of cars by foreign")
write_dta(auto, "sysuseauto3.dta")
However, not only the label does not appear the variable name using View(data)
, but also when I import back sysuseauto2.dta
and sysuseauto3.dta
into Stata the new variable is not labelled correctly.
Does anyone know how to approach this issue?
thanks a lot in advance for your help
Upvotes: 1
Views: 1331
Reputation: 1926
You were very close. The code below works on Stata 16.1 and R 4.0.3
with data.table_1.13.4
and haven_2.3.1
.
The only difference is the way you were assigning the label to the variable. Instead of
attr(auto, "haven_labelled") <- c(n_cars_by_foreign = "You New Label")
it should be attr(auto$n_cars_by_foreign, "label") <- "You New Label"
library(haven)
library(data.table)
# Get data from Stata (16.1)
auto<-read_dta("sysuseauto.dta")
# Gerenating new variable
auto <- setDT(auto)[,n_cars_by_foreign:=.N,by=.(foreign)]
# Extract the value label of "n_cars_by_foreign" (which is NULL)
attr(auto$n_cars_by_foreign, "label")
## NULL
# Adding the label value
attr(auto$n_cars_by_foreign, "label") <- "Number of cars by foreign"
#Check that value label variable is in place
attr(auto$n_cars_by_foreign, "label")
## [1] "Number of cars by foreign"
## Save the data
write_dta(auto, "sysuseauto2.dta")
Upvotes: 1