I'm trying to get the overall distance that an animal traveled by using a function that uses differences in lat long positions to output a distance traveled. Having some issues with nested loops.
The function ComputeDistance takes the arguments Lat1, Lat2, Long1, Long 2 in that order. Column 5 of DistTest contains latitude values and 6 contains longitude values.
So for the object "output", I'm trying to get sequential distances going through all 38 rows.
e.g.
ComputeDistance(DistTest[1,5],DistTest[2,5],DistTest[1,6],DistTest[2,6]
followed by:
ComputeDistance(DistTest[2,5],DistTest[3,5],DistTest[2,6],DistTest[3,6]
followed by:
ComputeDistance(DistTest[3,5],DistTest[4,5],DistTest[3,6],DistTest[4,6]
....
ComputeDistance(DistTest[37,5],DistTest[38,5],DistTest[37,6],DistTest[38,6]
I'm thinking that the problem is that the loop is going through every possible combination of DL and EL, not just going sequentially in order.
Below is the code I'm using currently.
## rows 1-37 and rows 2-38
DL <- 1:37
EL <- 2:38
## subsetting for one tagged animal
DistTest <- subset(Dispsum, Tag.ID == 1658)
## creating blank objects to save output in
testid <- c()
testdistance <- c()
for( j in DL){
for( k in EL){
output <- (ComputeDistance(DistTest[j,5], DistTest[k,5],DistTest[j,6], DistTest[k,6]))
Name <- 1658
testid <- rbind(testid, Name)
testdistance <- rbind(testdistance,output)
}
}