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

From: <Bill.Venables_at_csiro.au>
Date: Thu, 10 Jun 2010 14:57:13 +1000

This is a lazy way, and a slightly extravagant way if your memory is limited and you are dealing with large numbers of rows.

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

d <- dist(myMat)

dm <- as.matrix(d)
diag(dm) <- Inf
ij <- which(dm == min(dm), arr.ind = TRUE)[1,] ij

-----Original Message-----
From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org] On Behalf Of Jeff08 Sent: Thursday, 10 June 2010 1:30 PM
To: r-help_at_r-project.org
Subject: [R] Retrieving the 2 row of "dist" computations

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.


______________________________________________
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.
______________________________________________
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.
Received on Thu 10 Jun 2010 - 04:59:46 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 10 Jun 2010 - 05:20:30 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive