Removing Specific factor level from factor variable
Asked Answered
T

2

6

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?

Tradespeople answered 3/1, 2014 at 21:54 Comment(0)
K
8
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
Kiesha answered 3/1, 2014 at 22:5 Comment(3)
I do not think the as.character is needed.Escapement
You're right, but I'm always super wary of factors all of a sudden behaving like their underlying numbers as opposed to their "values". Clearly within the factor function expecting normal behavior is reasonable.Kiesha
as.data.frame messes with variable names, but check.names = FALSE helps apparently.Absorbent
E
0

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    
Escapement answered 3/1, 2014 at 22:24 Comment(1)
I think he wishes to drop the e level from all columnsKiesha

© 2022 - 2024 — McMap. All rights reserved.