R 不在子集中 [英] R Not in subset

查看:27
本文介绍了R 不在子集中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
从数据框中删除多个元素的标准方法

我知道在 R 中,如果您正在搜索另一个组的子集或根据 id 进行匹配,您会使用类似

I know in R that if you are searching for a subset of another group or matching based on id you'd use something like

subset(df1, df1$id %in% idNums1)

我的问题是如何做相反的事情或选择与 ID 向量不匹配的项目.

My question is how to do the opposite or choose items NOT matching a vector of ids.

我尝试使用 ! 但收到错误消息

I tried using ! but get the error message

subset(df1, df1$id !%in% idNums1)

我认为我的备份是这样的:

I think my backup is to do sometime like this:

matches <- subset(df1, df1$id %in% idNums1)
nonMatches <- df1[(-matches[,1]),]

但我希望有一些更有效的东西.

but I'm hoping there's something a bit more efficient.

推荐答案

表达式 df1$id %in% idNums1 产生一个逻辑向量.要否定它,您需要否定整个向量:

The expression df1$id %in% idNums1 produces a logical vector. To negate it, you need to negate the whole vector:

!(df1$id %in% idNums1)

这篇关于R 不在子集中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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