44% Distributed under GPL 2 or later
55
66\name {match }
7+ \title {Value Matching }
78\alias {match }
89\alias {\%in \%}
9- \title { Value Matching }
10+ \alias {\% notin \% }
1011\description {
1112 \code {match } returns a vector of the positions of (first ) matches of
1213 its first argument in its second.
1314
14- \code {\%in \%} is a more intuitive interface as a binary operator ,
15- which returns a logical vector indicating if there is a match or not
15+ \code {\%in \%} and \ code {\% notin \%} are more intuitive interfaces as binary operators ,
16+ returning a logical vector indicating if there is a match or not
1617 for its left operand.
1718}
1819\usage {
1920match(x , table , nomatch = NA_integer_ , incomparables = NULL )
2021
21- x \%in \% table
22+ x \%in \% table
23+ x \%notin \% table
2224}
2325\arguments {
2426 \item {x }{vector or \code {NULL }: the values to be matched.
@@ -35,21 +37,30 @@ x \%in\% table
3537\value {
3638 A vector of the same length as \code {x }.
3739
38- \code {match }: An integer vector giving the position in \code {table } of
40+ \describe {
41+ \item {\code {match }: }{An integer vector giving the position in \code {table } of
3942 the first match if there is a match , otherwise \code {nomatch }.
4043
4144 If \code {x [i ]} is found to equal \code {table [j ]} then the value
4245 returned in the \code {i }- th position of the return value is \code {j },
4346 for the smallest possible \code {j }. If no match is found , the value
4447 is \code {nomatch }.
45-
46- \code {\%in \%}: A logical vector , indicating if a match was located for
47- each element of \code {x }: thus the values are \code {TRUE } or
48- \code {FALSE } and never \code {NA }.
48+ }
49+ \item {\code {\%in \%}: }{A logical vector , indicating if a match was located for
50+ each element of \code {x }: thus the values are \code {TRUE } or
51+ \code {FALSE } and never \code {NA }. }
52+
53+ \item {\code {\%notin \%}: }{A logical vector , simply the negation of the
54+ corresponding \code {\%in \%} call. }
55+ }
4956}
5057\details {
5158 \code {\%in \%} is currently defined as \cr
52- \code {" \% in\% " <- function (x , table ) match(x , table , nomatch = 0 ) > 0 }
59+ \code {`\%in\%` <- function (x , table ) match(x , table , nomatch = 0L ) > 0L }
60+
61+ \code {\%notin \%} could succinctly be defined as \code {\link {Negate }(`\%in\%` )},
62+ but for symmetry and efficiency is \cr
63+ \code {`\%notin\%` <- function (x , table ) match(x , table , nomatch = 0L ) == 0L }
5364
5465 Factors , raw vectors and lists are converted to character vectors ,
5566 internally classed objects are transformed via \code {\link {mtfrm }}, and
@@ -79,7 +90,8 @@ x \%in\% table
7990 in different encodings but would agree when translated to UTF-8 (see
8091 \c ode{\l ink{Encoding}}).
8192
82- That \c ode{\% in\% } never returns \c ode{NA} makes it particularly
93+ That \c ode{\% in\% } and \c ode{\% notin\% } never return \c ode{NA} makes them
94+ particularly
8395 useful in \c ode{if} conditions.
8496}
8597\r eferences{
@@ -107,16 +119,16 @@ x \%in\% table
107119intersect # the R function in base is slightly more careful
108120intersect(1:10, 7:20)
109121
110- 1:10 \% in\% c(1,3,5,9)
122+ 1:10 \% in\% c(1,3,5,9) %
111123sstr <- c("c","ab","B","bba","c",NA,"@","bla","a","Ba","\% ")
112- sstr[sstr \% in\% c(letters, LETTERS)]
124+ sstr[sstr \% in\% c(letters, LETTERS)] %
113125
114- "\% w/o\% " <- function(x, y) x[! x \% in \% y] #-- x without y
115- (1:10) \% w/o\% c(3,7,12)
126+ "\% w/o\% " <- function(x, y) x[x \% notin \% y] #-- x without y
127+ (1:10) \% w/o\% c(3,7,12) %
116128## Note that setdiff() is very similar and typically makes more sense:
117129 c(1:6,7:2) \% w/o\% c(3,7,12) # -> keeps duplicates
118130setdiff(c(1:6,7:2), c(3,7,12)) # -> unique values
119-
131+ %
120132## Illuminating example about NA matching in complex values
121133r <- c(1, NA, NaN)
122134zN <- c(complex(real = NA , imaginary = r ),
0 commit comments