Johannes Radinger
2012-Dec-18 09:56 UTC
[R] Calculate geographic/euclidian distance between consecutive XY-pairs
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? best regards, /j
Ray Brownrigg
2012-Dec-18 10:17 UTC
[R] Calculate geographic/euclidian distance between consecutive XY-pairs
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.
Pascal Oettli
2012-Dec-18 10:24 UTC
[R] Calculate geographic/euclidian distance between consecutive XY-pairs
Hello, Here is a possibility: library(maptools) XY <- cbind(X=c(100,102,104,102,103),Y=c(12,14,14,13,16)) dst <- spDists(XY[1:4,],XY[2:5,],longlat=TRUE) ?spDists HTH, Pascal Le 18/12/2012 18:56, Johannes Radinger a ?crit :> 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? > > > 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. >
Johannes Radinger
2012-Dec-18 12:20 UTC
[R] Calculate geographic/euclidian distance between consecutive XY-pairs
Hi Ray, thank you very much. That one-line approach is what I was looking for. A very simple but very efficient way without loading any other packages. /j On Tue, Dec 18, 2012 at 11:17 AM, Ray Brownrigg <Ray.Brownrigg at ecs.vuw.ac.nz> wrote:> 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. > > >