Question

I have large data frames consisting of pairs of X and Y coordinates, and wish to calculate the Euclidean distances between consecutive coordinates (minimal size is around 2000 pairs of coordinates).

Thus, I want to calculate the distance from row 1 to 2, row 2 to 3, row 3 to 4, etc. This question nicely shows how to calculate the Euclidean distance between the first and last point for track data, but my data are closer to:

dff <- structure(list(A = c(0L, 0L, 0L, 0L, 0L, 0L), T = 0:5, X = c(668L, 670L, 672L, 674L, 676L, 678L), Y = c(259L, 259L, 259L, 259L, 259L, 260L), V = c(NA, 0, 0, 0, 0, 0)), .Names = c("A", "T", "X", "Y", "V"), row.names = c(NA, 6L), class = "data.frame")

It seems like there should be a way to create a loop to do this, but I'm uncertain about how to subscript this. Using dist() is computationally demanding for datasets of this size and in any case I am also unsure of how to extract matrix elements that are one-off from the diagonal.

Was it helpful?

Solution

Something like

sqrt(diff(dff$X)^2 + diff(dff$Y)^2)

should work. The key here is the diff function, which gives differences between consecutive items in a vector

OTHER TIPS

Another approach, just for fun:

sqrt(apply(apply(dff[,c("X","Y")], 2, diff)^2, 1, sum))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top