conflictcoder
conflictcoder

Reputation: 403

How do I export variable labels in R?

When I export a data.frame as a csv, I'd like to include the descriptions of the variables—labels I either created or inherited from SPSS. These labels are an easy way to keep track of what each variable encodes, and they display in Rstudio View(). Consider the following:

attach(iris)
setattr(iris$Sepal.Length, "label", "The length of a sepal in cm")
setattr(iris$Species, "label", "Species of iris")

Neither write.csv() or fwrite() seems to have a built-in option preserve these labels. Can you suggest a workaround or another function that will do so?

Upvotes: 3

Views: 2728

Answers (1)

I used the solution proposed here using the haven package and it worked. Here is the code:

haven::write_dta(df, "df_labelled.dta")

Upvotes: 0

Related Questions