asearch: Approximate Key-Value Search

View source: R/search.R

asearchR Documentation

Approximate Key-Value Search

Description

Search a set of values indexed by a sorted (non-decreasing) vector of keys. Finds the values corresponding to matches between the elements of the first argument and the keys. Approximate matching is allowed within a specified tolerance.

Usage

asearch(x, keys, values, tol = 0, tol.ref = "abs",
		nomatch = NA_integer_)

Arguments

x

A vector of values to be matched. Only integer, numeric, and character vectors are supported.

keys

A sorted (non-decreasing) vector of keys to match against. Only integer, numeric, and character vectors are supported.

values

A vector of values corresponding to the keys. Only numeric types are supported.

tol

The tolerance for matching. Must be nonnegative.

tol.ref

If 'abs', then comparison is done by taking the absolute difference. If 'x', then relative differences are used.

nomatch

The value to be returned in the case when no match is found.

Details

The algorithm is implemented in C and relies on binary search when the keys are sorted. The keys are sorted internally if necessary. See details for bsearch for matching behavior.

Value

A vector of the same length as x, giving the values corresponding to matching keys.

Author(s)

Kylie A. Bemis

See Also

bsearch, approx1

Examples

keys <- c(1.11, 2.22, 3.33, 5.0, 5.1)
values <- keys^1.11

asearch(2.22, keys, values) # 2.42359
asearch(3.0, keys, values) # NA
asearch(3.0, keys, values, tol=0.2, tol.ref="x") # 3.801133

kuwisdelu/matter documentation built on May 1, 2024, 5:17 a.m.