match(x, table, nomatch = NA_integer_, incomparables = NULL)
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. |
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)]