We may use rowwise
library(dplyr)
library(geosphere)
data %>%
rowwise %>%
mutate(EndLon = if(is.na(EndLon))
destPoint(c(StartLon, StartLat), Bearing, Length)[, 'lon'] else EndLon) %>%
ungroup
-output
# A tibble: 2 x 7
# Section StartLon StartLat EndLon EndLat Bearing Length
# <int> <dbl> <dbl> <dbl> <dbl> <int> <int>
#1 1 -132. 53.0 -132. 53.0 360 5
#2 2 -132. 53.0 -132. NA 360 10
data
data <- structure(list(Section = 1:2, StartLon = c(-132.4053, -132.4053
), StartLat = c(53.00704, 53.00714), EndLon = c(-132.4053, NA
), EndLat = c(53.00714, NA), Bearing = c(360L, 360L), Length = c(5L,
10L)), class = "data.frame", row.names = c(NA, -2L))
The issue would be that c(StartLon, StartLat)
would concatenate the whole column values from both of those column, and thereby the length
of one of the arguments for if_else
becomes different in length
than the rest. If we do the rowwise
, it is grouped by row
and we can use if/else
(which requires a input logical expression of length
1)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…