readstata13
readstata13 copied to clipboard
Support labelled vectors
At the moment, readstata13 is the only package available that can write compressed Stata .dta files. But the package does not play nicely with labelled vectors from the tidyverse labelled package. Instead, it treats them as numeric and, thus, removes all value and variable labels when it writes them to disk.
Any chance that readstata13 could support labelled vectors?
Could you give a small usage example? I have no experience with the labelled package.
Of course, see below.
# Load packages
library(tidyverse)
library(labelled)
library(readstata13)
library(here)
# Create data frame with labelled vectors
dta <-
data.frame(
lab_vct =
sample(c(1:3, 999), 100, replace = T) %>%
labelled(
labels =
c(
"a" = 1,
"b" = 2,
"c" = 3,
"dk" = 999
)
)
)
# Add variable label
var_label(dta$lab_vct) <- "Variable information here"
# Write to disk using save.dta13 (includes no variable/value labels)
save.dta13(data = dta, file = here("file.dta"), compress = T)
When we open the file in Stata we see that there are no variable or value labels:
![Screenshot 2021-02-13 at 10 19 27](https://user-images.githubusercontent.com/37049065/107847657-4fe0c100-6de5-11eb-87e6-13a5b5318c0f.png)
![Screenshot 2021-02-13 at 10 19 45](https://user-images.githubusercontent.com/37049065/107847660-52dbb180-6de5-11eb-9421-82bbd96e40b1.png)
Whereas if we save with write_dta from the haven package we can see them (though unfortunately cannot compress the Stata file, which can yield huge file sizes where data sets are large).
![Screenshot 2021-02-13 at 10 20 19](https://user-images.githubusercontent.com/37049065/107847675-6a1a9f00-6de5-11eb-88fc-062f8e343633.png)
![Screenshot 2021-02-13 at 10 20 26](https://user-images.githubusercontent.com/37049065/107847676-6d158f80-6de5-11eb-93d4-26cadeca37bc.png)
Finally, I would add that labelled vectors are often used when creating data for Stata as they allow one to specify specific label-value combinations (e.g. that "don't know" = 999) as opposed to factors numbering everything sequentially. They also allow you to add variable labels too.
It might be a good idea to add some code for labelled columns. I want to add some functions for working with labels anyway.
In the meantime you could just prepare you data manually before exporting:
# Load packages
library(tidyverse)
library(labelled)
library(readstata13)
library(here)
# Create data frame with labelled vectors
dta <-
data.frame(
lab_vct =
sample(c(1:3, 999), 100, replace = T) %>%
labelled(
labels =
c(
"a" = 1,
"b" = 2,
"c" = 3,
"dk" = 999
)
)
)
# Add variable label
var_label(dta$lab_vct) <- "Variable information here"
# Write to disk using save.dta13 (includes no variable/value labels)
save.dta13(data = dta, file = here("file.dta"), compress = T)
# Add another labelled variable
dta$num1 <- 1:100
var_label(dta$num1) <- "Numeric variable"
# Add a variable without label
dta$num2 <- 1:100
# Get vector of labelled variables
labeld_vars <- sapply(dta, is.labelled)
# And convert them to a factor
for(v in names(labeld_vars)[labeld_vars]) {
dta[[v]] <- to_factor(dta[[v]])
}
# Get variable labels
var_labs <- var_label(dta)
# Replace missing labels with ""
var_labs[sapply(var_labs, is.null)] <- ""
# Unlist and order variable labels
var_labs <- unlist(var_labs)[names(dta)]
# Save variable labels as attribute
attr(dta, "var.labels") <- var_labs
# Save dta-file
save.dta13(data = dta, file = here("file_with_labels.dta"), compress = T)
Thanks. I agree that supporting labelled vectors would be useful. While I could convert the labelled vectors to factors first, it's not useful in this case as it changes the numbers to be sequential. However, we need, for example, "Don't know" to be 999 in all cases and can't do this with factors that number each value label sequentially.
I think it is also possible to keep the numeric codes. I’ll take a look into that.
I did a rough implementation of labelled vectors in d8af2077bd8ba74c05795d2b53fb2be7bf9657bf . The code will probably change in the future, but you might give it a try:
# install from readstata13 from branch labels
remotes::install_github("sjewo/readstata13@label")
library(tidyverse)
library(labelled)
library(readstata13)
library(here)
# Create data frame with labelled vectors
dta <-
data.frame(
lab_vct =
sample(c(1:3, 999), 100, replace = T) %>%
labelled(
labels =
c(
"a" = 1,
"b" = 2,
"c" = 3,
"dk" = 999
)
)
)
# Add variable label
var_label(dta$lab_vct) <- "Variable information here"
# Get variable labels
var_labs <- var_label(dta)
# Replace missing labels with ""
var_labs[sapply(var_labs, is.null)] <- ""
# Unlist and order variable labels
var_labs <- unlist(var_labs)[names(dta)]
# Save variable labels as attribute
attr(dta, "var.labels") <- var_labs
# Write to disk using save.dta13 (includes now variable and value labels)
save.dta13(data = dta, file = here("file.dta"), compress = T)
Thanks for the package. It would be great if (missing) values such as 999 (don't know) could be recoded into Stata missing values such as .a Ideally, the user could supply the recoding rules: 999 = .a, 998 = .b etc