[R] search algorithm

From: <Ita.Cirovic-Donev_at_hypo-alpe-adria.com>
Date: Mon 13 Feb 2006 - 23:30:21 EST


I have a problem of finding a specific value in a column. For example, I have a matrix with say 2 columns

           X                       Y
1    -2.0341602   9.036689e-05
2    -1.4287230   1.807338e-04
3    -1.1194402   2.711007e-04
4    -1.0327582   3.614676e-04
5    -0.8130556   4.518344e-04
6    -0.7138212   5.422013e-04
7    -0.6634425   6.325682e-04
8    -0.6512083   7.229351e-04

9 -0.6176286 8.133020e-04
10 -0.5897241 9.036689e-04

Now if I have some value of x=-0.6523. I need to find a value in the X column that is the closest to my value of x then read off the row number and then take the corresponding value in column Y. What I am not sure is how to do the first search where I would search by decimal places and take the smallest absolute distance between the numbers. For example if he finds the first value which is correct in this case - and then 0 and then 6 and then 5 but now there is no 2 for that specific decimal place so he would calculate the distance between the one before and the one after and see which one is smaller. For that which is smaller would be the final X value. Can someone please give me a hint on how to proceed. Thanks.

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Mon Feb 13 23:36:23 2006

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:42:29 EST