I'm trying to access the variable labels (this is the description of the variable) from an SPSS por file with the haven package. I can do it just fine with the foreign package but I'd like to use haven. Any suggestions?
# Using foreign I can get the variable labels
with_foreign <- foreign::read.spss(mydata.por)
attr(with_foreign, "variable.labels")
# With haven I get null
with_haven <- haven::read_spss(mydata.por)
attr(with_haven, "variable.labels")
# Some things I've experimented with
labelled::var_label(with_haven) # NULL
attributes(with_haven) # Not useful
as_factor(with_haven$var1) # Gives me definitions for factor levels (not what I need)
How do I have SPSS show the variable names instead of the variable labels in the list of variables? Edit Options… Under the “General” tab, in the upper left corner, click on the radio button to change from showing the variable labels to showing the variable names.
To get the variable label, simply call var_label() . To remove a variable label, use NULL . In RStudio, variable labels will be displayed in data viewer.
Variable Labels: Variable labels are composed of a few words that describe what a variable represents. If the variable labels are properly formatted in SPSS, they will show in output tables and graphs, instead of variable names. Value Labels: Value labels are labels for coded variables in our dataset.
As stated in read_spss
labels are stored as attributes of each column rather than attributes of the data.frame
. Try
lapply(with_haven, function(x) attributes(x)$label)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With