I have a dataframe consisting of a series of timestamps with lat-lon point locations relating to animal GPS tracking data, grouped into separate trips made by each animal. For each timestamped lat-lon, I also have the distance of the point to the animals' home colony (in km).
I would like to classify each point with whether or not it occurred before or after the animal reached its maximum distance from its home colony.
The aim is to have a column in the dataframe stating where or not the timestamped lat-lon occurs during the outward section of the animals' trip (defined as all points before the animal reached maximum distance to its home colony) or the return section (all points that occurred after the animal reached its maximum distance from its home colony and before it returned to the colony).
Here is example data from 2 trips:
My desired output is as follows - the below table, with the addition of the 'Loc_Class' (location classification) column, where MAX = maximum distance from the colony, OUT = points falling before the animal reaches that MAX, and RET= points where the animal has reached the maximum distance away from the colony and is returning back to it.
Trip_ID | Timestamp | LON | LAT | Colony_lat | Colony_lon | Dist_to_Colony | Loc_Class |
---|---|---|---|---|---|---|---|
A | 18/01/2022 14:00 | -2.81698 | -69.831474 | -71.89 | 5.159 | 369.9948202 | MAX |
A | 18/01/2022 14:30 | -2.750411 | -69.811873 | -71.89 | 5.159 | 369.5644383 | RET |
A | 18/01/2022 15:00 | -2.736943 | -69.811022 | -71.89 | 5.159 | 369.2463158 | RET |
A | 18/01/2022 15:30 | -2.645026 | -69.804136 | -71.89 | 5.159 | 367.1665826 | RET |
A | 18/01/2022 16:00 | -2.56825 | -69.833432 | -71.89 | 5.159 | 362.7877481 | RET |
B | 18/01/2022 21:30 | -3.046828 | -69.784849 | -71.89 | 5.159 | 380.0350746 | OUT |
B | 18/01/2022 22:00 | -3.080154 | -69.765688 | -71.89 | 5.159 | 382.4142364 | OUT |
B | 19/01/2022 00:30 | -3.025742 | -69.634483 | -71.89 | 5.159 | 390.8078861 | MAX |
B | 19/01/2022 01:00 | -2.898522 | -69.672147 | -71.89 | 5.159 | 384.3511473 | RET |
B | 19/01/2022 01:30 | -2.907463 | -69.769916 | -71.89 | 5.159 | 377.173593 | RET |
library(tidyverse)
library(dplyr)
library(geosphere)
#load dataframe
df <- read.csv("Tracking_Data.csv")
#Great circle (geodesic) - add the great circle distance between the timestamped location and the animals' colony
df_2 <- df %>% mutate(dist_to_colony = distGeo(cbind(LON, LAT), cbind(Colony_lon, Colony_lat)))
#change distance from colony from m to km
df_2 <- df_2 %>% mutate(dist_to_colony = dist_to_colony/1000)
#find the point at which the maximum distance to colony occurs for each animals' trips
Max_dist_colony <- df_2 %>% group_by(TripID) %>% summarise(across(c(dist_to_colony), max))
#so now I need to classify each point using the 'Timestamp' and 'Dist_to_Colony' column and make a 'Loc_Class' column:
#example df
| Trip_ID | Timestamp | LON | LAT |Colony_lat|Colony_lon|Dist_to_Colony|
| -------- | -----------------|----------------------|--------- |--------- |------------- |
|A |18/01/2022 14:00 |-2.81698 |-69.831474 | -71.89 |5.159 |369.9948202 |
|A |18/01/2022 14:30 |-2.750411|-69.811873 | -71.89 |5.159 |369.5644383 |
|A |18/01/2022 15:00 |-2.736943|-69.811022 | -71.89 |5.159 |369.2463158 |
|A |18/01/2022 15:30 |-2.645026|-69.804136 | -71.89 |5.159 |367.1665826 |
|A |18/01/2022 16:00 |-2.56825 |-69.833432 | -71.89 |5.159 |362.7877481 |
|B |18/01/2022 21:30 |-3.046828|-69.784849 | -71.89 |5.159 |380.0350746 |
|B |18/01/2022 22:00 |-3.080154|-69.765688 | -71.89 |5.159 |382.4142364 |
|B |19/01/2022 00:30 |-3.025742|-69.634483 | -71.89 |5.159 |390.8078861 |
|B |19/01/2022 01:00 |-2.898522|-69.672147 | -71.89 |5.159 |384.3511473 |
|B |19/01/2022 01:30 |-2.907463|-69.769916 | -71.89 |5.159 |377.173593 |