Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match with negation

Tags:

r

This is really simple but I can't seem to locate it. I know R has a negated version of %in% that returns "not in." Obviously I could just use !(x %in% y) , but the language includes an already negated construct and I want to use it, goshdarnit.

So what's the function? Searches as well as %nin% and %notin% all fail.

Bonus internets to you if you benchmark your answer versus !(x %in% y) using the following sample data:

x <- sample( sample(letters,5), 10^3, replace=TRUE)
y <- sample( letters, 10^5, replace=TRUE)
like image 479
Ari B. Friedman Avatar asked Jul 03 '12 00:07

Ari B. Friedman


1 Answers

Just out of interest. Defining

"%w/o%" <- function(x, y) x[!x %in% y] 
'%ni%' <- Negate('%in%')

> benchmark(y[y%ni%x], y%w/o%x,replications=1000)
         test replications elapsed relative user.self sys.self user.child
2   y %w/o% x         1000    5.32 1.000000      4.60     0.70         NA
1 y[y %ni% x]         1000    5.34 1.003759      4.68     0.65         NA
  sys.child
2        NA
1        NA

Do i get a cookie?

like image 107
shhhhimhuntingrabbits Avatar answered Oct 23 '22 03:10

shhhhimhuntingrabbits