match
Match Items against a Table

Description

Returns a vector of the positions in table of the elements of x.

Usage

match(x, table, nomatch = NA_integer_, incomparables = NULL)

Arguments

x a vector of items to find in table. Missing values and Infs are allowed. Factors are treated as characters.
table the possible values in x. Missing values and Infs are allowed.
nomatch the value to return when an item in x matches no item in table. A useful alternative to NA is nomatch=0, which ignores unmatched items when the matched indices are used in subscripts (see the last example below). The value is coerced to an integer.
incomparables a vector of values that the function cannot match. Any value in x that matches a value in this vector is assigned the value specified by nomatch. If incomparables is NULL or FALSE, all values can be matched.

Details

Comparisons are done in the simplest mode that does not imply information loss. For example, if both x and table are numeric, then comparisons are numeric. If either x or table is a character string, then comparisons are of character strings. NaN is considered distinct from NA.
Value
returns an integer vector of the same length as x giving, for each element of x, the smallest i such that table[i] equals that element, or nomatch if there is no match.
See Also
regMatch, charmatch, pmatch, is.element, union, intersect, setdiff, matchlist.
Examples
x <- 1:10
primes <- c(2, 3, 5, 7, 11, 13, 17, 19, 23)
match(x, primes)
match(x, primes, incomp=2)       # match to all primes but 2
names <- c('Washington', 'New York', 'Sweden', 'Florida')

# change names to abbrevs (NA for 'Sweden') Sdatasets::state.abb[match(names, Sdatasets::state.name)]

# filter out non-state names ('Sweden') names[match(Sdatasets::state.name, names, 0)]

Package base version 6.1.1-7
Package Index