Description
The labelled()
and labelled_spss()
generator functions supports adding a labels
attribute, but they don’t have a built-in mechanism for adding a label
attribute. (The label
attribute hold a short descripton of the variable.) This despite the fact that haven
actually has very good support for the label
attribute, e.g. when both writing and reading SPSS files.
Suggestion: Add a label
argument to the labelled()
and labelled_spss()
functions. This would add the argument content as a label
attribute. Although the argument logically belongs next to the labels
argument, it’s probably best to add it as the last argument of the functions, in case people have used argument position when calling the functions.
Here’s a reprex illustrating that 1) the generator functions have no arguments for specifying the label
attribute, and 2) exporting and importing to the SPSS format does support the label
attribute (and correctly loses ‘unknown’ attributes):
library(haven)
library(tibble)
# No label argument
args(labelled)
#> function (x, labels)
#> NULL
args(labelled_spss)
#> function (x, labels, na_values = NULL, na_range = NULL)
#> NULL
# Example data
gen = labelled(c(0, 0, 1, 99),
labels = c(Female = 0, Male = 1))
attr(gen, "label") = "Patient gender"
attr(gen, "foo") = "bar"
d = tibble(gen)
# Write to SPSS and read resulting file
fname = tempfile()
write_sav(d, fname)
d2 = read_sav(fname)
# The `label` and `labels` attributes survived the conversion,
# but the `foo` attribute (correctly) did not
str(d2)
#> Classes 'tbl_df', 'tbl' and 'data.frame': 4 obs. of 1 variable:
#> $ gen:Class 'labelled' atomic [1:4] 0 0 1 99
#> .. ..- attr(*, "label")= chr "Patient gender"
#> .. ..- attr(*, "format.spss")= chr "F8.2"
#> .. ..- attr(*, "labels")= Named num [1:2] 0 1
#> .. .. ..- attr(*, "names")= chr [1:2] "Female" "Male"
I could probably create a PR if this is of interest.
Activity