Geographic / geospatial distance between 2 lists of lat/lon points (coordinates)

后端 未结 3 1934
情话喂你
情话喂你 2020-11-22 07:49

I have 2 lists (list1, list2) with latitude / longitudes of various locations. One list (list2) has locality names that list1

3条回答
  •  天涯浪人
    2020-11-22 08:16

    Credits to Martin Haringa for this solution on making this way easier when you need this function performed by traversing down a data frame on Mark Needham's blog

    library(dplyr)
    library(geosphere)
    
    df %>%
      rowwise() %>%
      mutate(newcolumn_distance = distHaversine(c(df$long1, df$lat1), 
                                                c(df$long2, df$lat2)))
    

    I tested using the two functions distm and distHaversine separately on large samples from real world datasets, and distHaversine seems to come out far faster than the distm function. I'm surprised as I thought the two were merely the same function in two formats.

提交回复
热议问题