I have a dataset with over 170
variables that looks as follows:
df <- data.frame(var1 = 1:3, var2 = 2:4, var3 = 2:4, var4 = 2:4, var5 = 2:4)
I have manually added variable values using Hmisc
:
library(Hmisc)
var.labels = c(var1 = "label 1",
var3 = "label 2",
var4="label 4" )
label(df) = as.list(var.labels[match(names(df), names(var.labels))])
Do note that not all variables have labels and that it is much easier for me to specify variable labels by name (var*
), rather than position given my large number of colums.
The problem is that when I save my dataset with write.csv(df,"df.csv")
or with write.dta(df,"df.dta")
my variable labels get lost.
How can I save the data in a way that can be re-imported into R and Stata with the labels restored?