views:

197

answers:

2

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!

+2  A: 

You can try out the read.spss function from the foreign package.

A rough and ready way to get rid of the labelled class created by spss.get

for (i in 1:ncol(x)) {
    z<-class(x[[i]])
    if (z[[1]]=='labelled'){
       class(x[[i]])<-z[-1]
       attr(x[[i]],'label')<-NULL
    }
}

But can you please give an example where labelled causes problems?

If I have a variable MAED in a data frame x created by spss.get, I have:

> class(x$MAED)
[1] "labelled" "factor"  
> is.factor(x$MAED)
[1] TRUE

So well-written code that expects a factor (say) should not have any problems.

Jyotirmoy Bhattacharya
Actually, this approach doesn't remove the `labelled` class. Here's an error:`Error in class(x[[i]]) <- z[-1] : invalid replacement object to be a class string`
aL3xa
It worked with a SPSS file I tried. Can you please link to a sample file where this fails? Or give the output offor (i in 1:ncol(x)) print(class(x[[i]]))where x is the imported data frame.
Jyotirmoy Bhattacharya
A: 

Well, I figured out that unclass function can be utilized to remove classes (who would tell, aye?!):

library(Hmisc)
# let's presuppose that variable x is gathered through spss.get() function
# and that x is factor
> class(x)
[1] "labelled" "factor"
> foo <- unclass(x)
> class(foo)
[1] "integer"

It's not the luckiest solution, just imagine back-converting bunch of vectors... If anyone tops this, I'll check it as an answer...

aL3xa