I'm having an issue with the write_dta function from the package haven.
My code goes like this:
lab_attr <- label$lab
attr(data, "label") <- lab_attr
attributes(data)
write_dta(data,"donnees/test_haven_1_data.dta", label = attr(data, "label"))
Which gives me the following error:
Error in validate_dta_label(label) : length(label) == 1 is not TRUE
All of my labels are under 80 characters.
Does anyone have any idea on how to fix this?
Thank you!
Have a nice day,
Cassandra
The way to store Stata variable labels as a data frame attribute is to assign to the attributes of each column, rather than of the data frame.
For example, using the iris
data:
data <- iris
names(data)
# "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species"
If you just wanted to edit one label you could do:
attr(data$Sepal.Length, "label") <- "new label"
If you want to create labels that explain what the variables are in a loop, you can do:
# Set labels which replace each . in the column name with a space
for(col in names(data)) {
attr(data[[col]], "label") <- gsub("\\.", " ", col)
}
# Print attributes
sapply(data, attr, "label")
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# "Sepal Length" "Sepal Width" "Petal Length" "Petal Width" "Species"
Once this is done, you do not need to set the label
argument in write_dta()
. It automatically writes the label attribute to the Stata labels. So in your case you can just do:
write_dta(data,"donnees/test_haven_1_data.dta")