[R] Retrieving the 2 row of "dist" computations

Jeff08 jefferyding at gmail.com
Thu Jun 10 05:30:24 CEST 2010


Dear R Gurus,

As you probably know, dist calculates the distance between every two rows of
data. What I am interested in is the actual two rows that have the least
distance between them, rather than the numerical value of the distance
itself.

For example, If the minimum distance in the following sample run is d[14],
which is .3826119, and the rows are 4 & 6. I need to find a generic way to
retrieve these rows, for a generic matrix of NRows (in this example NRows=7)

NCols=5
NRows=7 
myMat<-matrix(runif(NCols*NRows), ncol=NCols) 

d<-dist(myMat)

          1         2         3         4         5         6
2 0.7202138                                                  
3 0.7866527 0.9052319                                        
4 0.6105235 1.0754259 0.8897555                              
5 0.5032729 1.0789359 0.9756421 0.4167131                    
6 0.6007685 0.6949224 0.3826119 0.7590029 0.7994574          
7 0.9751200 1.2218754 1.0547197 0.5681905 0.7795579 0.8291303

e<-sort.list(d)
e<-e[1:5]  ##Retrieve minimum 5 distances

[1] 14 16  4 18  5
-- 
View this message in context: http://r.789695.n4.nabble.com/Retrieving-the-2-row-of-dist-computations-tp2249844p2249844.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list