I know that there is a similar question about that here
Any ideas of how should I approach this?
CodePudding user response:
If you want to calc distance by bird_ID
, then you should likely left_join
the locations
onto the original data before calculating the distance.
distances <- dt %>%
left_join(locations, by = "bird_ID") %>% # NEW
group_by(bird_ID) %>%
select(bird_ID, # suppress an auto-add message
longitude, latitude, date,
longitude.mean, latitude.mean) %>% # added so we have the data per-bird_ID
mutate(
Dist = geosphere::distHaversine(
cbind(longitude.mean, latitude.mean), # removed `location$`
cbind(longitude, latitude))
) %>%
tidyr::drop_na()
# # A tibble: 2 x 7
# # Groups: bird_ID [1]
# bird_ID longitude latitude date longitude.mean latitude.mean Dist
# <chr> <dbl> <dbl> <chr> <dbl> <dbl> <dbl>
# 1 350E 40.2 64.7 2022-05-02 52.1 68.5 674850.
# 2 350E 40.2 64.7 2022-05-02 52.1 68.5 674840.