# [R] shortest distance between two point pattern

Bill.Venables at csiro.au Bill.Venables at csiro.au
Sat Feb 9 06:38:25 CET 2008

```With points in R^2 it's often more transparent to do this directly in
the complex plane.

set.seed(20080209)  # today

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))

z0 <- complex(, x0, y0)
z1 <- complex(, x1, y1)
dz <- outer(z0, z1, function(z0, z1) Mod(z0-z1))

(d <- min(dz))                     # the min. dist

m <- which(dz == d)[1]
(i <- ((m-1) %% length(x0)) + 1)	# row of first data set
(j <- ((m-1) %/% length(x0)) + 1)	# row of second data set

sqrt((x0[i]-x1[j])^2 + (y0[i]-y1[j])^2) # check

plot(c(z0, z1), asp = 1, xlab = "x", ylab = "y",
col = rep(c("red", "blue"), c(length(z0), length(z1))))
segments(x0[i], y0[i], x1[j], y1[j])

Bill Venables
CSIRO Laboratories
PO Box 120, Cleveland, 4163
AUSTRALIA
Office Phone (email preferred): +61 7 3826 7251
Fax (if absolutely necessary):  +61 7 3826 7304
Mobile:                         +61 4 8819 4402
Home Phone:                     +61 7 3286 7700
mailto:Bill.Venables at csiro.au
http://www.cmis.csiro.au/bill.venables/

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Gabor Grothendieck
Sent: Saturday, 9 February 2008 2:47 PM
To: Milton Cezar Ribeiro
Cc: R-help
Subject: Re: [R] shortest distance between two point pattern

Try using dist:

ix <- 1:nrow(df.1)
m <- as.matrix(dist(rbind(df.1, df.2)))[ix, -ix]
which(min(m) == m, arr = TRUE)

On Feb 8, 2008 11:01 PM, Milton Cezar Ribeiro
<milton_ruser at yahoo.com.br> wrote:
> 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.merg
e.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],c
ode=0,col=3,lwd=2,lty=1)
>
> Any help are welcome
>
> Miltinho
> Brazil.
>
>
>
>  para armazenamento!
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
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