Last data update: 2014.03.03

R: Index of a vector nearest in value to a supplied value
nearestR Documentation

Index of a vector nearest in value to a supplied value

Description

Returns the index of a vector which contains the value closest to an arbitary value

Usage

nearest(x, xval, outside=FALSE, na.rm=FALSE)

Arguments

x

vector of values

xval

value to find the nearest value in x to

outside

if not set to TRUE the function returns an error if xval is outside the range of x - default FALSE

na.rm

NA behaviour: TRUE drops cases with NA's, FALSE stops function with a warning if NA's are detected: default=FALSE

Value

returns an integer:

index

the index of x with the value nearest to xval

Acknowledgements

Written in collaboration with A.M.Pollard <mark.pollard@rlaha.ox.ac.uk> with the financial support of the Natural Environment Research Council (NERC) grant GR3/11395

Note

The vector doesn't have to be in any particular order - this routine will just give the index of the nearest number. The only inconsistancy is that if the value of xval are not strictly within the range of the vector the function will return an error. To prevent this call with the outside=TRUE flag enabled. If there are many values which match the 'nearest' value then the function will return a vector of their indicies.

Author(s)

David Lucy <d.lucy@lancaster.ac.uk> http://www.maths.lancs.ac.uk/~lucy/
Robert Aykroyd <r.g.aykroyd@leeds.ac.uk>http://www.amsta.leeds.ac.uk/~robert/

Examples

# make up a vector
x <- c(1,2,2,2,2,2,3,4,5,6,7,8,9,10)
# conventional useage - xval within range should return 9
nearest(x, 4.7)
# xval - outside the range of x should return 14
nearest(x, 12.7, outside=TRUE)
# many 'nearest' values in x - should return - 2 3 4 5 6 
nearest(x, 1.7)
# make x[3] an NA
x[3] <- NA
# returns - 2 4 5 6 - by enabling na.rm
nearest(x, 1.7, na.rm=TRUE)

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(GenKern)
Loading required package: KernSmooth
KernSmooth 2.23 loaded
Copyright M. P. Wand 1997-2009
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/GenKern/nearest.Rd_%03d_medium.png", width=480, height=480)
> ### Name: nearest
> ### Title: Index of a vector nearest in value to a supplied value
> ### Aliases: nearest
> ### Keywords: arith
> 
> ### ** Examples
> 
> # make up a vector
> x <- c(1,2,2,2,2,2,3,4,5,6,7,8,9,10)
> # conventional useage - xval within range should return 9
> nearest(x, 4.7)
[1] 9
> # xval - outside the range of x should return 14
> nearest(x, 12.7, outside=TRUE)
[1] 14
> # many 'nearest' values in x - should return - 2 3 4 5 6 
> nearest(x, 1.7)
[1] 2 3 4 5 6
> # make x[3] an NA
> x[3] <- NA
> # returns - 2 4 5 6 - by enabling na.rm
> nearest(x, 1.7, na.rm=TRUE)
[1] 2 4 5 6
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>