[R] avoiding loops

Liaw, Andy andy_liaw at merck.com
Fri Jan 28 22:54:53 CET 2005


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




More information about the R-help mailing list