从因子变量中删除特定因子水平 [英] Removing Specific factor level from factor variable

查看:199
本文介绍了从因子变量中删除特定因子水平的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框架,其中有几个变量具有5个因子级别。我想只删除其中一个级别。首先,我将该级别的所有实例分配给NA,然后使用等式命令来摆脱空白级别。



但是对于我的数据框架中的一个变量,我不想删除的一个级别没有任何意见。有没有办法去除一个特定的因素级别,而不仅仅是空的。



这是一个可重现的例子

  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 ,e,b)

这样设置一个像我的数据框。现在我想删除e级的所有实例,然后将其作为可能的级别删除。我使用下面的代码来执行此操作。

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

问题是当我使用它们也从var3中删除等级b。我不想删除所有变量的级别b。我已经寻找一种方法去删除一个特定的级别,但没有找到答案。任何人都可以告诉我如何去除一个特定的因素级别?我最理想的是一个等离子体命令,我可以告诉你只是删除e级。这样的功能是否存在?

解决方案

  str(
as.data。框架(
lapply(
df2,
function(x)factor(as.character(x))levels = levels(x)[levels(x)!=e])
)))
#'data.frame':10 obs。的3个变量:
#$ var1:因子w / 4级别a,b,c,d:1 2 3 4 NA 1 2 3 4 NA
#$ var2 :因素w / 4级别a,b,c,d:NA 4 3 2 1 NA 4 3 2 1
#$ var3:因子w / 4级别a, c,d,b:1 2 3 NA 1 2 3 NA 1 2


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?

解决方案

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

这篇关于从因子变量中删除特定因子水平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆