The R Project SVN R

Rev

Rev 7553 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

\name{match}
\title{Value Matching}
\usage{
match(x, table, nomatch=NA)
x \%in\% table
}
\alias{match}
\alias{\%in\%}
\arguments{
\item{x}{the values to be matched.}
\item{table}{the values to be matched against.}
\item{nomatch}{the value to be returned in the case when no match is found.}}
\description{
  \code{match}: If \code{x[i]} is found to equal \code{table[j]}
  then the value returned in the \code{i}-th position of the return value is
  \code{j}.  If no match is found, the value is \code{nomatch}.
  
  \code{\%in\%}: A utility function, currently defined as \cr
  \code{"\%in\%" <- function(x, table) match(x, table, nomatch = 0) > 0}
  allowing
  an intuitive usage and returning a logical vector of length \code{length(x)}.
}
\seealso{
\code{\link{pmatch}} and \code{\link{charmatch}} for (\emph{partial})
string matching, \code{\link{match.arg}}, etc for function argument matching.
}
\examples{
## The intersection of two sets :
intersect <- function(x, y) y[match(x, y, nomatch = 0)]
intersect(1:10,7:20)

1:10 \%in\% c(1,3,5,9)
sstr <- c("c","ab","B","bba","c","@","bla","a","Ba","\%")
sstr[sstr \%in\% c(letters,LETTERS)]

"\%w/o\%" <- function(x,y) x[!x \%in\% y] #--  x without y
(1:10) \%w/o\% c(3,7,12)
}
\keyword{manip}
\keyword{logic}