Re: [R] Minimum distance

2005-04-17 Thread Douglas Bates
Williams, Michael wrote:
G'day, I have a matrix 2 x 500 populated with all the distances
between a list of airports and a list of towers. What I'm having trouble
doing is finding the closest airport to each tower, Any help would be
greatly appreciated.
Regards,
Michael Williams
Sounds like you want to apply the function which.min to the rows of the 
matrix.  See

?apply
and
?which.min
__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


RE: [R] Minimum distance

2005-04-17 Thread Mulholland, Tom
Since it is not clear exactly what you require, I have assumed that what you 
are looking for is the minimum value for each row or column depending upon 
which is airports and which is the towers.

Is this what you are looking for

x - runif(100)
dim(x) - c(5,20)
apply(x,1,function(y) which(y == min(y)))

The only issue I see here is that there may be more than one minimim in which 
case the value returned will be a list with all of the matched values which 
would need to be processed

 x - trunc(runif(100) * 10)
 dim(x) - c(5,20)
 apply(x,1,function(y) which(y == min(y)))
 
If you did not have a method for processing you could order the towers in some 
preferred order and just take the first match

apply(x,1,function(y) head(which(y == min(y)),1))

If you are talking about a matrix where you need to find the shortest path, you 
might want to look at the package e1071. I think the function is called 
allShortestPaths

Tom

 -Original Message-
 From: [EMAIL PROTECTED]
 [mailto:[EMAIL PROTECTED] Behalf Of 
 Williams, Michael
 Sent: Monday, 18 April 2005 7:58 AM
 To: r-help@stat.math.ethz.ch
 Subject: [R] Minimum distance
 
 
 G'day, I have a matrix 2 x 500 populated with all the distances
 between a list of airports and a list of towers. What I'm 
 having trouble
 doing is finding the closest airport to each tower, Any help would be
 greatly appreciated.
 
 Regards,
 Michael Williams
 
 
 
   [[alternative HTML version deleted]]
 
 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide! 
 http://www.R-project.org/posting-guide.html


__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html