[R] A %nin% operator?

Erik Iverson eriki at ccbr.umn.edu
Thu Aug 5 17:26:25 CEST 2010


Just FYI, the Hmisc package has had an implementation of %nin% for some 
time now.

Ken Williams wrote:
> Sometimes I write code like this:
> 
>> qf.a <- subset(qf, pubid %in% c(104, 106, 107, 108))
>> qf.b <- subset(qf, !pubid %in% c(104, 106, 107, 108))
> 
> and I get a little worried that maybe I've remembered the precedence rules
> wrong, so I change it to
> 
>> qf.a <- subset(qf, pubid %in% c(104, 106, 107, 108))
>> qf.b <- subset(qf, !(pubid %in% c(104, 106, 107, 108)))
> 
> and pretty soon my code looks like fingernail clippings (or Lisp) and I'm
> thinking about precedence rather than my original task.  So I write a %nin%
> operator which I define as:
> 
>> `%nin%` <- function (x, table) match(x, table, nomatch = 0L) == 0L
> 
> and then I'm happy again.
> 
> I wonder, would something like this find a home in core R?  Or is that too
> much syntactic sugar for your taste?
>



More information about the R-help mailing list