Re: [R] Find the closest value in a list or matrix

From: Daniel Malter <daniel_at_umd.edu>
Date: Wed, 09 Jul 2008 14:03:21 -0400


x=c(1:100)
your.number=5.43

which(abs(x-your.number)==min(abs(x-your.number)))

Best,
Daniel



cuncta stricte discussurus

-----Ursprüngliche Nachricht-----

Von: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org] Im Auftrag von R_Learner
Gesendet: Wednesday, July 09, 2008 11:28 AM An: r-help_at_r-project.org
Betreff: [R] Find the closest value in a list or matrix

I have a long list of numbers [3.4,5.4,3.67,....], and I basically want to find the index of the number closest to this number that I have, let's say 5.43. How would I do this without writing a for loop (I have to do this many times for several lists)? Is there a "lookup" function in R?

Thanks!
--

View this message in context:
http://www.nabble.com/Find-the-closest-value-in-a-list-or-matrix-tp18363290p 18363290.html
Sent from the R help mailing list archive at Nabble.com.



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 09 Jul 2008 - 18:19:54 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 09 Jul 2008 - 20:33:41 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive