# [R] Easy way?

Martin Maechler maechler at stat.math.ethz.ch
Mon Jun 26 10:14:56 CEST 2000

```>>>>> "Uwe" == Uwe Ligges <ligges at statistik.uni-dortmund.de> writes:

>> Yves Gauvreau wrote:
>>
>> Hi,
>>
>> Say I have this vector x <- sort(rnorm(n)). I'd like to know if
>> there is a nice way to find out the index of the nearest x to say
>> 1.5?

Uwe> I would try:
which(abs(x - 1.5) == min(abs(x - 1.5)))

(and Jan Schelling gives the same solution in "modular" steps
without recomputing   "abs(x - 1.5)".)

This is fine and fast enough for moderate data size.
However, since version 1.1

help(which) has

>>      `Logic', `which.min' for the index of the minimum or maximum.
---------
and  which.min(), which.max() are really the functions you want to use
here.

Martin Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO D10	Leonhardstr. 27
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1228			<><
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```