which.nearest: Which Nearest

View source: R/which.nearest.R

which.nearestR Documentation

Which Nearest

Description

Find values of one vector that are nearest to values in another vector.

Usage

which.nearest(x, y)

Arguments

x

vector of values to be compared against.

y

vector of values to examine relative to x. May be of length 1.

@return For each value in y, returns index of value of x which is nearest to y in absolute value. In the case of ties, the function returns the first index of x. If nearest value is min(x) or max(x), a warning is issued. NAs and NaNs in x are ignored; NAs and NaNs in y are returned.

Author(s)

Tim Gerrodette tim.gerrodette@noaa.gov

Examples

x <- sort(sample(1:100, 20))
y <- sort(sample(min(x):max(x), 5))
i <- which.nearest(x, y)
x
y
x[i]

swfscMisc documentation built on Sept. 8, 2023, 5:55 p.m.