Search code examples
classrimportlabelspss

Remove variable labels attached with foreign/Hmisc SPSS import functions


As usual, I got some SPSS file that I've imported into R with spss.get function from Hmisc package. I'm bothered with labelled class that Hmisc::spss.get adds to all variables in data.frame, hence want to remove it.

labelled class gives me headaches when I try to run ggplot or even when I want to do some menial analysis! One solution would be to remove labelled class from each variable in data.frame. How can I do that? Is that possible at all? If not, what are my other options?

I really want to bypass reediting variables "from scratch" with as.data.frame(lapply(x, as.numeric)) and as.character where applicable... And I certainly don't want to run SPSS and remove labels manually (don't like SPSS, nor care to install it)!

Thanks!


Solution

  • A belated note/warning regarding class membership in R objects. The correct method for identification of "labelled" is not to test for with an is function or equality {==) but rather with inherits. Methods that test for a specific location will not pick up cases where the order of existing classes are not the ones assumed.

    You can avoid creating "labelled" variables in spss.get with the argument: , use.value.labels=FALSE.

    w <- spss.get('/tmp/my.sav', use.value.labels=FALSE, datevars=c('birthdate','deathdate'))
    

    The code from Bhattacharya could fail if the class of the labelled vector were simply "labelled" rather than c("labelled", "factor") in which case it should have been:

    class(x[[i]]) <- NULL  # no error from assignment of empty vector
    

    The error you report can be reproduced with this code:

    > b <- 4:6
    > label(b) <- 'B Label'
    > str(b)
    Class 'labelled'  atomic [1:3] 4 5 6
      ..- attr(*, "label")= chr "B Label"
    > class(b) <- class(b)[-1]
    Error in class(b) <- class(b)[-1] : 
      invalid replacement object to be a class string