Skip to main content
2 of 2
fixed formatting
user avatar
user avatar

Calculate distance between X/Y coordinates by individual in R

In R, I'd like to calculate the straight-line distance between each capture of an animal in my dataset - no need to take into account the curvature of the earth. I have a dataset that looks like this:

record.nbr<- c(1:8)
individual<-  c(1,2,1,2,1,3,1,2)
x<-c(167685,167945,167685,153985,167685,158675,167645,167667)
y<-c(9876548,9879248,9876838,9596548,9926548,9878578,9876548,9166548)
julian.date<-c(125,126,127,127,128,129,130,130)
captures<-data.frame(cbind(record.nbr, individual, x, y, julian.date))

Here each "individual" represents a unique animal, and x/y points represent locations where the animals were captured. x/y points are UTM coordinates. Some individuals are caught many times, while other individuals are only captured once.

I'd like to calculate the distance (and the mean date) between recaptures by individual. In the output table, each row would summarize the distance between consecutive recaptures of an individual, plus the mean date between recaptures, like so:

individual first.rec  second.rec  distance  mean.date
1          1             3        V         126
1          3             5        W         127.5
1          5             7        X         129
2          2             4        Y         126.5
3          4             6        Z         128
Luke
  • 397
  • 1
  • 4
  • 9