In my limited experience, R
does not use labels as often as Stata
. Still you can do it. You need two packages: expss
and haven
:
In r
, use expss::apply_labels
to assign variable labels and value labels. I only use variable labels for the purpose of demonstration:
df <- data.frame(year = c(2017, 2018, 2019),
age = c(25, 30, 40),
bmi = c(23.2, 28.3, 32))
# assign labels in r
df = expss::apply_labels(df,
year = "Survey year",
age = "Age, years",
bmi = "BMI, kg/m2"
)
# export to Stata
haven::write_dta(df, "havenstata.dta")
In Stata
, you can check:
. des
Contains data from C:\...\havenstata.dta
obs: 3
vars: 3 24 Dec 2019 22:17
size: 72
------------------------------------------------------------------------------------------------------
storage display value
variable name type format label variable label
------------------------------------------------------------------------------------------------------
year double %10.0g Survey year
age double %10.0g Age, years
bmi double %10.0g BMI, kg/m2
------------------------------------------------------------------------------------------------------
Sorted by:
Although there are some solutions, I think it might be easier to label data in Stata
after exported to Stata
.