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!
A belated note/warning regarding class membership in R objects. The correct method for identification of "labelled" is not to test for with an
isfunction or equality {==) but rather withinherits. 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.
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:
The error you report can be reproduced with this code: