5

I have some tracking data and I want to calculate the bearing over the course of the track. For two points we can use function from the fossil package:

# earth.bear(long1, lat1, long2, lat2)
earth.bear(-10.54427, 52.11112, -10.55493, 52.10944)
# 255.6118

However, this won't work for more than two points. Here's some sample data:

tracks <- read.table(text = 
"latitude,  longitude
52.111122,  -10.544271
52.10944,   -10.554933
52.108898,  -10.558025
52.108871,  -10.560946
52.113991,  -10.582005
52.157223,  -10.626506
52.194977,  -10.652878
52.240215,  -10.678817
52.26421,   -10.720366
52.264015,  -10.720642", header = TRUE, sep = ",")

1 Answer 1

4

Try this:

sum(
  sapply(1:(nrow(tracks) - 1), function(i){
    earth.bear(tracks$longitude[i], tracks$latitude[i],
               tracks$longitude[i+1], tracks$latitude[i+1] )

  })
)

# 2609.871
1
  • 1
    I'll edit my question to make it clearer because it's not the sum of the bearings I needed, rather I wanted to loop over more than two data points. Which your answer also provides. Thanks a lot.
    – adkane
    Feb 16, 2016 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.