Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable labels in the R package Haven with SPSS

Tags:

import

r

r-haven

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)
like image 365
ZRoss Avatar asked Feb 16 '18 01:02

ZRoss


People also ask

How do I have SPSS show the variable names instead of the variable labels in the list of variables?

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.

How do I get the label of a variable in R?

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.

What is the label of a variable in SPSS?

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.


1 Answers

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)
like image 79
Ista Avatar answered Sep 20 '22 06:09

Ista