J. Doe
J. Doe

Reputation: 1740

Extracting labels from SPSS files in R

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!

Upvotes: 5

Views: 2286

Answers (2)

drhnis
drhnis

Reputation: 113

Try labelled packages to get the labels:

data_label <- t(as.data.frame(labelled::var_label(data))) 

write.csv(data_label, "data_label.csv")

Upvotes: 4

Juan Bosco
Juan Bosco

Reputation: 1430

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])

Upvotes: 5

Related Questions