R Not in subset

RSubset

R Problem Overview


> 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.

R Solutions


Solution 1 - R

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

!(df1$id %in% idNums1)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionscreechOwlView Question on Stackoverflow
Solution 1 - RAri B. FriedmanView Answer on Stackoverflow