[R] Selecting closest values
jim holtman
jholtman at gmail.com
Sun Mar 22 19:30:09 CET 2009
If we assume the distance is linear, this might work:
> x
ID loc
1 1 144
2 2 144
3 3 140
4 4 126
5 5 120
6 6 112
7 7 100
8 8 99
9 9 91
10 11 90
11 12 90
12 13 89
13 15 86
14 16 85
15 17 85
16 18 80
17 19 79
18 20 79
19 21 78
20 22 78
21 23 76
22 24 74
23 25 73
24 26 72
25 27 71
26 28 68
27 29 68
28 30 68
> x.outer <- outer(x$loc, x$loc, function(a,b) abs(a - b))
> # set diag to Inf so a point is not closest to itself
> diag(x.outer) <- Inf
> # assume that you choose the 8th element, here are the closest IDs
> x$ID[order(x.outer[,8])]
[1] 7 9 11 12 13 6 15 16 17 18 19 20 5 21 22 23 24 25 4 26 27 28
29 30 3 1 2 8
>
>
On Sun, Mar 22, 2009 at 2:04 PM, P_M <pmartinu at broadpark.no> wrote:
>
> Yes, of course.
>
> ID loc
> 1 144
> 2 144
> 3 140
> 4 126
> 5 120
> 6 112
> 7 100
> 8 99
> 9 91
> 11 90
> 12 90
> 13 89
> 15 86
> 16 85
> 17 85
> 18 80
> 19 79
> 20 79
> 21 78
> 22 78
> 23 76
> 24 74
> 25 73
> 26 72
> 27 71
> 28 68
> 29 68
> 30 68
> . .
> . .
> 185 -22
>
>
> P_M wrote:
>>
>> Hi
>> I have a table with ID (1 to 183) and Location (144 to -22).
>> My problem is that I want to select the 10 ID's that are closest in
>> Location to ID 1, ID 2 and so on.
>> Also, some ID have the same Location. Say, if 11 ID's are closest to ID
>> 100 I want to randomly choose one of the ID's to select 10 ID's total.
>>
>> Thank you
>>
>>
>>
>>
>
> --
> View this message in context: http://www.nabble.com/Selecting-closest-values-tp22647126p22648754.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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list