I have animal survey data from transects. Transects are divided into sections. There are lat/lon data for the start/endpoints of some sections but not others, and I want to calculate the start/endpoints for sections where these values are missing. Missing start/endpoints should be calculated using the section bearing (degrees), section length (m).
Example data:
Section | StartLon | StartLat | EndLon | EndLat | Bearing | Length |
---|---|---|---|---|---|---|
1 | -132.4053 | 53.00704 | -132.4053 | 53.00714 | 360 | 5 |
2 | -132.4053 | 53.00714 | NA | NA | 360 | 10 |
I'm trying to use destPoint (geosphere) to calculate the missing start/endpoints (NAs). The output of destPoint looks like:
lon lat
[1,] -132.4053 53.00701
My code:
data %>%
mutate(EndLon = if_else(is.na(EndLon), destPoint(c(StartLon, StartLat), Bearing, Length), EndLon))
data %>%
mutate(EndLat = if_else(is.na(EndLat), destPoint(c(StartLon, StartLat), Bearing, Length), EndLat))
My code gives this error:
Error: Problem with `mutate()` input `test`.
x Wrong length for a vector, should be 2
i Input `test` is `if_else(...)`
I think the error is because the output of destPoint is two values (lon and lat), and the mutated column can only hold one value. Maybe there's a way to use select() so that only lon or lat goes into the mutated column?
Hoping for a dplyr solution.