vctrs (version 0.1.0)

vec_match: Find matching observations across vectors

Description

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)

vec_in(needles, haystack)

Arguments

needles, haystack

Vector of needles to search for in vector haystack. haystack should usually be unique; if not vec_match() will only return the location of the first match.

needles and haystack are coerced to the same type prior to comparison.

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, missing values are not considered to be equal, i.e. NA == NA is not TRUE. This behaviour would be unappealing here, so these functions consider all NAs to be equal. (Similarly, all NaN are also considered to be equal.)

Performance

These functions are currently slightly slower than their base equivalents. This is primarily because they do a little more checking and coercion in R, which makes them both a litter safer and more generic. Additionally, the C code underlying vctrs has not yet been implemented: we expect some performance improvements when that happens.

Details

vec_in() is equivalent to %in%; vec_match() is equivalen to match().

Examples

Run this code
# NOT RUN {
hadley <- strsplit("hadley", "")[[1]]
vec_match(hadley, letters)

vowels <- c("a", "e", "i", "o", "u")
vec_match(hadley, vowels)
vec_in(hadley, vowels)

# Only the first index of duplicates is returned
vec_match(c("a", "b"), c("a", "b", "a", "b"))
# }

Run the code above in your browser using DataCamp Workspace