[R] Calculate geographic/euclidian distance between consecutive XY-pairs

Ray Brownrigg Ray.Brownrigg at ecs.vuw.ac.nz
Tue Dec 18 11:17:14 CET 2012


On 18/12/2012 10:56 p.m., Johannes Radinger wrote:
> Hi,
>
> I have a dataframe containing 3 columns:
>
> data.frame(X=c(100,102,104,102,103),Y=c(12,14,14,13,16),Time=c(1,2,3,4,5))
>
> where X and Y are coordinates and "Time" refers to an index of a timestep.
> Now I would like to get the distance between the consecutive timesteps.
>
> This should actually provide a vector of length=4, representing the euclidian
> resp. geopgraphic distance between the first and the second, and the second
> and the third timestep and so on...
>
> Is there a simple way to calculate this and get the resulting vector
> as a result?
> Or can anyone give an example?
How about something like:
coords <- 
data.frame(X=c(100,102,104,102,103),Y=c(12,14,14,13,16),Time=c(1,2,3,4,5))
with(coords, sqrt(diff(X)^2 + diff(Y)^2))

Ray Brownrigg
>
> best regards,
> /j
>
> ______________________________________________
> 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.




More information about the R-help mailing list