1

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"
SiH
  • 1,378
  • 4
  • 18
  • If you only need to do this to a single numeric variable then `as.numeric(PersonID)` should work - however if you need a more comprehensive solution for other variables or a whole dataset you might want to look at solutions here: https://stackoverflow.com/questions/52532995/remove-attributes-from-dataframe and https://stackoverflow.com/questions/53627377/how-to-erase-all-attributes. Because you are using haven you could also look at the `zap_()` family of functions in haven (https://haven.tidyverse.org/reference/zap_formats.html) in this case you'd use `zap_format(data)` and `zap_label(data)` – Emily Kothe Mar 04 '21 at 01:53

1 Answers1

3

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))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thanks. In the haven documentation, there are function that start with zap_xxxx() (https://haven.tidyverse.org/reference/index.html). Can you please tell if these can be used – SiH Mar 04 '21 at 01:58
  • Yes, they can be used as well and seem more appropriate for this task. `zap_labels` removes labels from the data whereas `zap_formats` remove formats. We have to use them together to remove both the attributes. Updated the answer to include that. – Ronak Shah Mar 04 '21 at 02:05