Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting labels from SPSS files in R

Tags:

database

r

label

I imported a SPSS file into R using the haven package. As you can see on the next image, all variables have labels associated with them (e.g. "2016 YEAR OF ADMINISTRATION"):

Labels

I'm trying to read those labels, however using this line of code returns NULL:

attr(X36799_0001_Data[,15], "label")

ant this line of code also returns NULL:

attributes(X36799_0001_Data)$variable.labels

Any info on what I'm doing wrong would be hugely appreciated. Thank you!

like image 583
J. Doe Avatar asked Mar 07 '23 20:03

J. Doe


1 Answers

Just change the way you are subsetting, and it should work.

attr(X36799_0001_Data[[15]], "label")

The explanation of this has to do with the way R subsets. An in depth explanation is here: Subsetting - Advanced R.

You can also use the package labelled to deal with SPSS labels. In this case, using the var_label.

var_label(X36799_0001_Data[, 15])
like image 76
Juan Bosco Avatar answered Mar 09 '23 10:03

Juan Bosco