swfscMisc (version 1.3)

which.nearest: 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.

Examples

Run this code
# NOT RUN {
x <- sort(sample(1:100, 20))
y <- sort(sample(min(x):max(x), 5))
i <- which.nearest(x, y)
x
y
x[i]
# }

Run the code above in your browser using DataLab