Search code examples
rtidyverser-haven

remove the extra information (attr) after reading file spss file using read_sav


I used read_sav() to read SPSS file in R.

How do I remove the extra information (attr).

I don't know how to create reprex for this question, but I have a sample below. I wish to remove attr from the column PersonID and convert it into normal dataframe/tibble Thanks

'data.frame':   543 obs. of  1 variable:
 $ PersonID  : num  1 2 3 4 5 6 7 8 9 10 ...
  ..- attr(*, "label")= chr "Person identifier"
  ..- attr(*, "format.spss")= chr "F8.0"

Solution

  • To remove all the attributes of the column you can use :

    attributes(data$PersonID) <- NULL
    

    To remove only specific ones you can do :

    attr(data$PersonID, 'format.spss') <- NULL
    

    To remove all attributes from all the columns :

    data[] <- lapply(data, function(x) {attributes(x) <- NULL;x})
    

    We can also use zap_labels and zap_formats from haven.

    library(haven)
    data <- zap_formats(zap_labels(data))