Search code examples
rdistancegeosphere

Error: Assigned data `distHaversine(dtrav)` must be compatible with existing data


I'm just trying to calculate distance travelled using longitude/latitude points. I have 2983223 rows of data. I keep getting this error and my distance column repeats the same value throughout the 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.

Solution

  • You should separate start point and end point matrices, otherwise distHaversine calculates distances between rows, which leads to n-1 distances for n rows, hence the error message.

    df$distance = distHaversine(cbind(c(df$start_lng,df$start_lat)),
                                cbind(c(df$end_lng, df$end_lat)))