[R] easier way to do this without a loop? (successive euclidean distances between points)
Henrique Dallazuanna
wwwhsd at gmail.com
Mon Oct 6 18:58:21 CEST 2008
Try this:
diag(rdist(rbind(z, 0), rbind(0, z))[,-(1:2)])
On Mon, Oct 6, 2008 at 1:49 PM, stephen sefick <ssefick at gmail.com> wrote:
> a <- c(1:10)
> b <- c(.5, .6, .9, 10, .4, 3, 4, 9, 0, 11)
> d <- c(21:30)
>
> z <- data.frame(a,b,d)
> library(fields)
> results <- c()
> for(i in 1:(length(rownames(z))-1)){
> results[i] <- rdist(z[i,], z[(i+1),])
> }
>
> results.1 <- data.frame(results)
> f <- rownames(z)
> r <- f[-1]
> rownames(results.1) <- r
> colnames(results.1) <- f[1]
>
> this does what I want it to do - is there an easier/generic way of
> doing this. I will be using this to calculate euclidean distances
> between successive time steps on ordination scores.
> thanks
>
> --
> Stephen Sefick
> Research Scientist
> Southeastern Natural Sciences Academy
>
> Let's not spend our time and resources thinking about things that are
> so little or so large that all they really do for us is puff us up and
> make us feel like gods. We are mammals, and have not exhausted the
> annoying little problems of being mammals.
>
> -K. Mullis
>
> ______________________________________________
> 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.
>
--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O
More information about the R-help
mailing list