R: Which Nearest

which.nearest {swfscMisc} | R 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. `NA`s and `NaN`s in `x` are ignored; `NA`s and `NaN`s 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]

[Package swfscMisc version 1.2 Index]