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.
It would be helpful is you supplied a subset of the data so that we could see what you expect. The 'outer' On Sun, Mar 22, 2009 at 1:48 PM, P_M <pmartinu at broadpark.no> 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-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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
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.
P_M <pmartinu at broadpark.no> [Sun, Mar 22, 2009 at 06:48:36PM CET]:> > 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.In the general case, the nn function from the RANN package (not part of CRAN but found by rseek) does what you want, but for the one-dimensional case this might be overkill. -- Johannes H?sing There is something fascinating about science. One gets such wholesale returns of conjecture mailto:johannes at huesing.name from such a trifling investment of fact. http://derwisch.wikidot.com (Mark Twain, "Life on the Mississippi")
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) <- idHere'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.