[R] Selecting closest values

Bill.Venables at csiro.au Bill.Venables at csiro.au
Mon Mar 23 00:14:36 CET 2009


Here is one way you might find useful

> id <- 1:183
> lo <- sample((-22):144, 183, rep = TRUE)
> ds <- outer(lo, lo, function(x,y) abs(x-y))
> rs <- t(apply(ds, 2, function(x) id[order(x)][2:11]))
> rownames(rs) <- id

Here's what I got

> head(rs)
  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
1  118   23  125  182  183    8   86  119  141   175
2   27  167  176    4  126   45  113   81  144   153
3   43    7   18   60  148   11   12  150   85   163
4  126   27  167  176    2   45  144  153   48    55
5   15   46   86  141  175   23  125  183    1   118
6   58   78  139   13   66  131  156  181  117   157

If you want anything more random in the case of ties than this, you will have to work a bit harder on the code.  

W.

Bill Venables
http://www.cmis.csiro.au/bill.venables/ 


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of P_M
Sent: Monday, 23 March 2009 1:32 AM
To: r-help at r-project.org
Subject: [R] Selecting closest values


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-tp22647126p22647126.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.




More information about the R-help mailing list