[R] Selecting closest values
Frank E Harrell Jr
f.harrell at vanderbilt.edu
Mon Mar 23 04:32:29 CET 2009
Bill.Venables at csiro.au wrote:
> 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
>
>
>
Also take a look at the whichClosest function in the Hmisc package. But
whichClosest cheats by calling Fortran.
Cheers
Frank
--
Frank E Harrell Jr Professor and Chair School of Medicine
Department of Biostatistics Vanderbilt University
More information about the R-help
mailing list