join two datasets with lat and lon using a geo_left_join but only take the match with the minimum distance in r

main.r
library(geosphere)

# create example datasets
data1 <- data.frame(id = c(1, 2, 3),
                     lat = c(40, 45, 30),
                     lon = c(-90, -95, -100))

data2 <- data.frame(id = c(101, 102, 103),
                     lat = c(41, 44, 29),
                     lon = c(-89, -94, -99))

# calculate distances between all combinations of points
dist_matrix <- geosphere::distHaversine(data1[, c("lon", "lat")], data2[, c("lon", "lat")])

# find indices of minimum distances for each record in data1
min_dist_indices <- apply(dist_matrix, 1, which.min)

# create final dataset by combining data1 and matched data2
final_data <- cbind(data1, data2[min_dist_indices,])

# print final_data
print(final_data)
719 chars
23 lines

related categories

gistlibby LogSnag