Re: [R] minimum distances

From: Ben Bolker <bolker_at_ufl.edu>
Date: Thu, 20 Mar 2008 16:57:02 +0000 (UTC)

Dave Depew <ddepew <at> sciborg.uwaterloo.ca> writes:

>
> Hi,
> I have a question about computing shortest Euclidean distances between
> two data frames of spatial points...
>
> I have 2 dataframes (not yet converted to spatial class)
>
> >Sewers<-data.frame(x=c(10,20,35,50),y=c(100,150,200,300))
> >transect <- data.frame(x=seq(from=0, to=50, by=1),y=seq(from=100,
> to=150, by=1))
>
> I would like to be able to compute the shortest distance from the
> transect points the nearest sewer (Euclidean distance)
>
> I've tried a number of different loops, but so far ave had no luck.
>
> Any help would be appreciated
>

Sewers<-data.frame(x=c(10,20,35,50),y=c(100,150,200,300)) transect <- data.frame(x=0:50,y=100:150)

dist <- sqrt((outer(Sewers$x,transect$x,"-"))^2+

             (outer(Sewers$y,transect$y,"-"))^2) mindist = apply(dist,2,min)
closest = apply(dist,2,which.min)

library(MASS)
eqscplot(Sewers,pch=16,col=1:4)
points(transect,col=closest)



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 20 Mar 2008 - 16:59:21 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 20 Mar 2008 - 18:30:24 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