如何将特定因子水平删除到 r 中的缺失值?

how to remove a specific factor level to missing value in r?

我得到了一个因子数据集df,看起来像这样:

df <- data.frame(attend = c("yes", "no", "no", "iap", "yes", "yes", "iap"),
           sex = c("male", "female", "female", "male", "female", "male", "female"))

df$attend <- as.factor(df$attend)
df$sex <- as.factor(df$sex)

df
attend sex
yes male
no female
no female
iap male
yes female
yes male
iap female

我只想从 attend 变量中删除 iap 级别。(我不想删除整行,我正在寻找的是删除级别 iap,以至于在变量attend)

下变成缺失值

我尝试使用下面的代码将其删除,但出现错误提示: UseMethod("droplevels") 错误: 没有适用于 'droplevels' 的方法应用于 class“字符”的对象。

df$attend <- droplevels(levels(df$attend)[4])

如果有人能提供帮助,将不胜感激。

你可以改变变量的levels -

levels(df$attend)[levels(df$attend) == 'iap'] <- NA
df

#  attend    sex
#1    yes   male
#2     no female
#3     no female
#4   <NA>   male
#5    yes female
#6    yes   male
#7   <NA> female

这也会自动降低 'iap' 等级。

levels(df$attend)
#[1] "no"  "yes"

这里我们也可以使用forcats::fct_recode将特定值转为NA

df$attend <- forcats::fct_recode(df$attend, NULL = 'iap')

另一种基础 R 解决方案是使用 exclude:

df$attend <- factor(df$attend, exclude = "iap")
  attend    sex
1    yes   male
2     no female
3     no female
4   <NA>   male
5    yes female
6    yes   male
7   <NA> female