[R] avoiding loops

Liaw, Andy andy_liaw at merck.com
Sat Jan 29 02:28:17 CET 2005


I didn't read the question carefully.  You were asking all pairwise
distances between the two sets, right?

This isn't the most efficient way to do it, but it seems OK for problem of
the size you stated:

> pairdist <- function(m1, m2) {
+     idx1 <- rep(1:nrow(m1), each=nrow(m2))
+     idx2 <- rep(1:nrow(m2), nrow(m1))
+     d <- sqrt(rowSums((m1[idx1,] - m2[idx2,])^2))
+     dim(d) <- c(nrow(m1), nrow(m2))
+     d
+ }
> d1 <- matrix(rnorm(3e3), 1e3, 3)
> d2 <- matrix(rnorm(3e3), 1e3, 3)
> system.time(dm <- pairdist(d1, d2), gcFirst=TRUE)
[1] 0.73 0.13 0.92   NA   NA
> str(dm)
 num [1:1000, 1:1000] 2.44 2.21 2.45 1.58 3.58 ...

Andy


> From: Liaw, Andy
> 
> Doesn't this work?
> 
> > d1 <- data.frame(x=rnorm(100), y=rnorm(100), z=rnorm(100))
> > d2 <- data.frame(x=rnorm(100), y=rnorm(100), z=rnorm(100))
> > dist.12 <- sqrt(rowSums((d1 - d2)^2))
> 
> Andy
> 
> > From: dax42
> > 
> > Hi again,
> > 
> > thanks a lot for the quick answer. I just forgot the comma, always 
> > these stupid mistakes...
> > 
> > Anyways, as I said before, I have two data.frames containing 
> > about 1000 
> > rows and I would like to avoid looping through all of them...
> > 
> > In each data.frame are coordinates (x,y,z), so every row is 
> > giving the 
> > information on one single point.
> > I would like to calculate the distance from each point in the 
> > one frame 
> > to every point in the second...
> > But how to do this without loops, how to do it quickly? I would 
> > appreciate it very much, if someone would introduce me to the 
> > high art 
> > of R - avoiding loops :-).
> > 
> > Cheers, Dax.
> > 
> > ______________________________________________
> > R-help at 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 at 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
> 
> 
> --------------------------------------------------------------
> ----------------
> Notice:  This e-mail message, together with any attachments, 
> contains information of Merck & Co., Inc. (One Merck Drive, 
> Whitehouse Station, New Jersey, USA 08889), and/or its 
> affiliates (which may be known outside the United States as 
> Merck Frosst, Merck Sharp & Dohme or MSD and in Japan, as 
> Banyu) that may be confidential, proprietary copyrighted 
> and/or legally privileged. It is intended solely for the use 
> of the individual or entity named on this message.  If you 
> are not the intended recipient, and have received this 
> message in error, please notify us immediately by reply 
> e-mail and then delete it from your system.
> --------------------------------------------------------------
> ----------------
>




More information about the R-help mailing list