How to select a data set using several exceptions?

How to combine all state names in one vector, and not list all logical exceptions separately? I found the% notin% function on the CRAN website, but R does not recognize it as a legit function.

indata <- indata[which(indata$STATE != "GU" & indata$STATE != "WY" & indata$STATE != "KS" & indata$STATE != "ME" & indata$STATE != "MT" & indata$STATE != "ND" &), ] 

Thanks.

+4
source share
2 answers
 indata[!indata$STATE %in% c("GU", "WY", "KS", "ME", "MT", "ND"), ] 

EDIT: @CarlWitthoft, believe it or not, I actually had the following in a private package for a while

 `%notin%` <- function (x, table) x[!x %in% table] 

However, I never think that I will use it until I type it in a long way. Also, using this code makes my code less accessible. I did not know about

 operators:::`%!in%` 

which is only the second half of %notin%

+5
source

Try again:

 library(operators) x%!in%y #works fine 
+3
source

Source: https://habr.com/ru/post/1412062/


All Articles