vec_in() returns a logical vector based on whether needle is found in
haystack. vec_match() returns an integer vector giving location of
needle in haystack, or NA if it's not found.
Usage
vec_match(
needles,
haystack,
...,
na_equal = TRUE,
needles_arg = "",
haystack_arg = ""
)
vec_in(
needles,
haystack,
...,
na_equal = TRUE,
needles_arg = "",
haystack_arg = ""
)Arguments
- needles, haystack
Vector of
needlesto search for in vector haystack.haystackshould usually be unique; if notvec_match()will only return the location of the first match.needlesandhaystackare coerced to the same type prior to comparison.- ...
These dots are for future extensions and must be empty.
- na_equal
If
TRUE, missing values inneedlescan be matched to missing values inhaystack. IfFALSE, they propagate, missing values inneedlesare represented asNAin the return value.- needles_arg, haystack_arg
Argument tags for
needlesandhaystackused in error messages.
Value
A vector the same length as needles. vec_in() returns a
logical vector; vec_match() returns an integer vector.
Missing values
In most cases places in R, missing values are not considered to be equal,
i.e. NA == NA is not TRUE. The exception is in matching functions
like match() and merge(), where an NA will match another NA.
By vec_match() and vec_in() will match NAs; but you can control
this behaviour with the na_equal argument.
Dependencies
vec_cast_common()with fallback
Examples
hadley <- strsplit("hadley", "")[[1]]
vec_match(hadley, letters)
#> [1] 8 1 4 12 5 25
vowels <- c("a", "e", "i", "o", "u")
vec_match(hadley, vowels)
#> [1] NA 1 NA NA 2 NA
vec_in(hadley, vowels)
#> [1] FALSE TRUE FALSE FALSE TRUE FALSE
# Only the first index of duplicates is returned
vec_match(c("a", "b"), c("a", "b", "a", "b"))
#> [1] 1 2
