# [ACCEPTED]-Remove variable labels attached with foreign/Hmisc SPSS import functions-spss

Here's how I get rid of the labels altogether. Similar 5 to Jyotirmoy's solution but works for a 4 vector as well as a data.frame. (Partial 3 credits to Frank Harrell)

```
clear.labels <- function(x) {
if(is.list(x)) {
for(i in 1 : length(x)) class(x[[i]]) <- setdiff(class(x[[i]]), 'labelled')
for(i in 1 : length(x)) attr(x[[i]],"label") <- NULL
}
else {
class(x) <- setdiff(class(x), "labelled")
attr(x, "label") <- NULL
}
return(x)
}
```

Use as follows:

```
my.unlabelled.df <- clear.labels(my.labelled.df)
```

**EDIT**

Here's 2 a bit of a cleaner version of the function, same 1 results:

```
clear.labels <- function(x) {
if(is.list(x)) {
for(i in seq_along(x)) {
class(x[[i]]) <- setdiff(class(x[[i]]), 'labelled')
attr(x[[i]],"label") <- NULL
}
} else {
class(x) <- setdiff(class(x), "labelled")
attr(x, "label") <- NULL
}
return(x)
}
```

You can avoid creating "labelled" variables 6 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 5 code from Bhattacharya could fail if the 4 class of the labelled vector were simply 3 "labelled" rather than c("labelled", "factor") in 2 which case it should have been:

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

The error 1 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
```

You can try out the `read.spss`

function from the `foreign`

package.

A 6 rough and ready way to get rid of the `labelled`

class 5 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 4 where `labelled`

causes problems?

If I have a variable 3 `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 2 well-written code that expects a factor 1 (say) should not have any problems.

Suppose:

```
library(Hmisc)
w <- spss.get('...')
```

You could remove the labels of a 4 variable called "var1" by using:

```
attributes(w$var1)$label <- NULL
```

If you 3 also want to remove the class "labbled", you 2 could do:

```
class(w$var1) <- NULL
```

or if the variable has more than 1 one class:

```
class(w$var1) <- class(w$var1)[-which(class(w$var1)=="labelled")]
```

Hope this helps!

Well, I figured out that `unclass`

function can be 4 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 3 not the luckiest solution, just imagine 2 back-converting bunch of vectors... If anyone 1 tops this, I'll check it as an answer...

More Related questions

We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.