[R] "haven" - read_spss: How to avoid extracting value labels instead of long labels?

Dimitri Liakhovitski dimitri.liakhovitski at gmail.com
Thu Nov 12 17:56:12 CET 2015


Hello!

I don't have an example file, but I think my question should be clear
without it.
I have an SPSS file. I read it in using 'haven':

library(haven)
spss1 <- read_spss("SPSS_Example.sav")

I created a function that extracts the long labels (in SPSS - "Label"):

fix_labels <- function(x, TextIfMissing) {
      val <- attr(x, "label")
      if (is.null(val)) TextIfMissing else val
}
longlabels <- sapply(spss1, fix_labels, TextIfMissing = "NO LABLE IN SPSS")

This function is supposed to create a vector of long labels and
usually it does, e.g.:

str(longlabels)
 Named chr [1:64] "Serial number" ...
 - attr(*, "names")= chr [1:64] "Respondent_Serial" "weight" "r7_1" "r7_2" ...

However, I just got an SPSS file with 92 columns and ran exactly the
same function on it. Now, I am getting not a vector, but a list

str(longlabels)
List of 92
 $ VEHRATED      : chr "VEHICLE RATED"
 $ RESPID        : chr "RESPONDENT ID"
 $ RESPID8       : chr "8 DIGIT RESPONDENT NUMBER"

An observation about the structure of longlabels here: those columns
that do NOT have a long lable in SPSS but DO have Values (value
labels) - for them my function grabs their value labels, so that now
my long label is recorded as a numeric vector with names, e.g.:

 $ AWARE2        : Named num [1:2] 1 2
  ..- attr(*, "names")= chr [1:2] "VERY/SOMEWHAT FAMILIAR" "NOT AT ALL FAMILIAR"

Question: How could I avoid the extraction of the Value Labels for the
columns that have no long labels?

Thank you very much!
-- 
Dimitri Liakhovitski



More information about the R-help mailing list