Search notes:
R function: match
Match returns the index of the
first element in a
vector with a specific value:
n <- c( 11, 18, 7, 38, 42, 98, 66, 7, 18, 42, 56, 13)
match(42, n)
#
# [1] 5
It also possible to look-up the values of a vector in another vector:
values <- c('one', 'two', 'three', 'four', 'five', 'four', 'three', 'two', 'one');
words <- c('five', 'two', 'four')
match(words, values);
#
# 5 2 4
See also
which(…)
returns the indices in a logical vector that are
TRUE
.
The
%in%
operator is defined as
"%in%" <- function(x, table) match(x, table, nomatch = 0) > 0
.