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

Henrique Dallazuanna wwwhsd at gmail.com
Wed Jul 9 20:01:03 CEST 2008


Try this:

which.min(abs(x - 5.43))

where x is your vector of numbers.



On Wed, Jul 9, 2008 at 12:28 PM, R_Learner <sschiang8 at gmail.com> wrote:
>
> 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-tp18363290p18363290.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.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list