我只是想用经度/纬度点来计算行驶的距离。我有2983223行数据。我一直收到这个错误,并且我的distance列在整个df中重复相同的值。
library(geosphere)
dtrav <- matrix(c(df$start_lng,df$start_lat), c(df$end_lng, df$end_lat), nrow=2983223,ncol=2)
df$distance <- distHaversine(dtrav)
Error: Assigned data `distHaversine(dtrav)` must be compatible with existing data.
x Existing data has 2983223 rows.
x Assigned data has 2983222 rows.
ℹ Only vectors of size 1 are recycled.
发布于 2021-05-28 12:14:12
应该将起点矩阵和终点矩阵分开,否则distHaversine
会计算行之间的距离,这会导致n
行的n-1
距离,因此会出现错误消息。
df$distance = distHaversine(cbind(c(df$start_lng,df$start_lat)),
cbind(c(df$end_lng, df$end_lat)))
https://stackoverflow.com/questions/67732275
复制相似问题