R Not in subset [duplicate]

放肆的年华 提交于 2019-12-03 02:34:25

问题


Possible Duplicate:
Standard way to remove multiple elements from a dataframe

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)

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.


回答1:


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

!(df1$id %in% idNums1)


来源:https://stackoverflow.com/questions/9852832/r-not-in-subset

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