数据帧D1中的分类变量V1可以有从A到z的字母表示的值。我想创建一个子集D2,其中不包括一些值,比如B、N和t。基本上,我想要一个与%中的%相反的命令

D2 = subset(D1, V1 %in% c("B", "N", "T"))

当前回答

使用negate from purrr也可以快速而整洁地达到目的:

`%not_in%` <- purrr::negate(`%in%`)

例如,用法是,

c("cat", "dog") %not_in% c("dog", "mouse")

其他回答

你可以使用!运算符基本上使任何TRUE为FALSE,每个FALSE为TRUE。所以:

D2 = subset(D1, !(V1 %in% c('B','N','T')))

编辑: 你也可以自己创建一个操作符:

'%!in%' <- function(x,y)!('%in%'(x,y))

c(1,3,11)%!in%1:10
[1] FALSE FALSE  TRUE

下面是在dplyr中使用过滤器的一个版本,它通过否定逻辑with !应用了与接受答案相同的技术:

D2 <- D1 %>% dplyr::filter(!V1 %in% c('B','N','T'))

如果你看%in%的代码

 function (x, table) match(x, table, nomatch = 0L) > 0L

然后你应该能够写出你的相反版本。我使用

`%not in%` <- function (x, table) is.na(match(x, table, nomatch=NA_integer_))

另一种方法是:

function (x, table) match(x, table, nomatch = 0L) == 0L

在Frank Harrell的R效用函数包中,他有一个%nin% (not In),它完全符合最初的问题。不需要重新发明轮子。

包崩溃内置了:%!在%中。