Domanda

I have a data frame that has several variables that have 5 factor levels. I want to delete only one of those levels. First I assigned all instances of of that level to NA, and then used the droplevels command to get rid the empty levels.

However for one variable in my data frame one of the levels I don't want dropped has no observations in it. Is there a way to remove only a specific factor level, and not just the empty ones.

Here is a reproducible example

df <- data.frame(var1=rep(letters[1:5],2),var2=rep(letters[5:1],2),var3=c("a","c","d","e","a","c","d","e","a","c"))
levels(df$var3)<-c("a","c","d","e","b")

This sets up a data frame like mine. Now I want to remove all instances of the level e, and then drop it as a possible level. I do this with the code below.

df2<-replace(df, df=="e",NA)
df2<-droplevels(df2)

The problem is when I use droplevels it drops level b from var3 also. I don't want to remove level b just level e from all of the variables. I have looked for a way to remove just a specific level, but have not found the answer. Can anyone show me how to remove just a specific factor level? What I would ideally like is a droplevels command that I can tell to just remove level e. Does such a function exist?

È stato utile?

Soluzione

str(
  as.data.frame(
    lapply(
      df2, 
      function(x) factor(as.character(x), levels=levels(x)[levels(x) != "e"])
) ) )
# 'data.frame':  10 obs. of  3 variables:
# $ var1: Factor w/ 4 levels "a","b","c","d": 1 2 3 4 NA 1 2 3 4 NA
# $ var2: Factor w/ 4 levels "a","b","c","d": NA 4 3 2 1 NA 4 3 2 1
# $ var3: Factor w/ 4 levels "a","c","d","b": 1 2 3 NA 1 2 3 NA 1 2

Altri suggerimenti

I don't understand why you don't just use droplevels on the factor column of interest:

df2$var2 <- droplevels(df2$var2)

> lapply(df2, levels)
$var1
[1] "a" "b" "c" "d" "e"

$var2
[1] "a" "b" "c" "d"

$var3
[1] "a" "c" "d" "e" "b"

Explanation: droplevels is generic and there are both methods for factor and dataframe objects.

> methods(droplevels)
[1] droplevels.data.frame droplevels.factor    
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top