match and %in% are two very commonly-used function in R. So, what’s the difference of them?
First, how to use them — (copy from R manual)
match returns a vector of the positions of (first) matches of its first argument in its second.
%in% is a more intuitive interface as a binary operator, which returns a logical vector indicating if there is a match or not for its left operand.
match(x, table, nomatch = NA_integer_, incomparables = NULL)x %in% table
 1 1 0 1 5 1 2 4
 10 9 8 7 6 5 4 3 2 1
 10 10 NA 10 6 10 9 7
> a %in% b
 TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE
So, if two vectors are overlapped like
To get the overlapped part in order of a, use a[a %in% b], even though there are duplicates in the overlapped part. However, this does not work for match, since match() only returns the first match of a in b. For example,
 NA NA NA NA NA 5 8 NA 7 1
> match(b,a, nomatch=0)
 0 0 0 0 0 5 8 0 7 1
> a[match(b,a, nomatch=0)]
 5 4 2 1
even using ‘nomatch=0’, the final command still returns 4 elements, not the overlapped ones.
To leave a comment
for the author, please follow the link and comment on their blog: One Tip Per Day
offers daily e-mail updates
news and tutorials
on topics such as: Data science
, Big Data, R jobs
, visualization (ggplot2
), programming (RStudio
, Web Scraping
) statistics (regression
, time series
) and more...
If you got this far, why not subscribe for updates
from the site? Choose your flavor: e-mail
, or facebook