# Re: [R] shortest distance between two point pattern

From: huber <huber_at_ebi.ac.uk>
Date: Sat, 09 Feb 2008 16:23:50 +0000

Dear Miltinho,

there is also the function "matchpt" in the Biobase package (in Bioconductor) that seems to do what you want (in n dimensions).

It's written in C, and the implementation is simple and of complexity n*m. (For larger problems, there are more efficient nearest neighbor search algorithms, but I am not aware whether or where in R.). This one works well for medium sized problem as in your example.

Best wishes
Wolfgang

Wolfgang Huber EBI/EMBL Cambridge UK http://www.ebi.ac.uk/huber

Milton Cezar Ribeiro scripsit:
> Hi R-experts.
>
> I am working in a R-code where I have two datasets with x and y coordinates on each dataset.
> I intent to identify the shortest distance between this two datasets. I wrote a short code to do that.
> But when I join the datasets to compute the distances, the merge function run so slowly.
> I need only to identify the index of rows from each dataset related to the shortest distance.
>
> x0<-rnorm(n=500,mean=1,sd=runif(1))
> y0<-rnorm(n=500,mean=3,sd=runif(1))
> x1<-rnorm(n=700,mean=8,sd=runif(1))
> y1<-rnorm(n=700,mean=5,sd=runif(1))
> df.0<-cbind(x0,y0)
> df.1<-cbind(x1,y1)
> plot(df.0,xlim=range(c(x0,x1)),ylim=range(c(y0,y1)))
> points(df.1,col=2)
> rm(x0,x1,y0,y1)
>
> #merge two data.frames of points
> #### IT SPEND many time
> df.merge<-merge(df.0,df.1,all=T)
> #compute distances between each pair of points
> attach(df.merge)
> df.merge\$distance<-((x0-x1)^2+(y0-y1)^2)^0.5
> detach(df.merge)
> #identify the minimum distance
> df.merge.distance.min<-min(df.merge\$distance)
> #select the pair of points (x0,y0,x1,y1) with shortest distance
> df.merge.distance.min.subset<-subset(df.merge,df.merge\$distance<=df.merge.distance.min)
> #trace a arrow between the points with shortest distance
> arrows(df.merge.distance.min.subset[1,1],df.merge.distance.min.subset[1,2],df.merge.distance.min.subset[1,3],df.merge.distance.min.subset[1,4],code=0,col=3,lwd=2,lty=1)
>
> Any help are welcome
>
> Miltinho
> Brazil.
>
>

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 Sat 09 Feb 2008 - 16:32:33 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 Sat 09 Feb 2008 - 18:30:13 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.