Removing Specific factor level from factor variable

℡╲_俬逩灬. 提交于 2019-12-21 04:49:35

问题


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?


回答1:


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



回答2:


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    


来源:https://stackoverflow.com/questions/20913379/removing-specific-factor-level-from-factor-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!