Найти ближайший к центру центроид относительно легко с пакетом sf. Вам нужно будет преобразовать ваш фрейм данных в объект sf, как показано ниже.
library(tidyverse)
library(sf)
#> Linking to GEOS 3.6.2, GDAL 2.2.3, PROJ 4.9.3
centroid <- data.frame (longitude =c( -1.482880, -1.485735),
latitude = c( 54.89935, 54.89935),
ID = c(1,2))
locations <- data.frame(longitude = c(-1.482156, -1.482318, -1.482129, -1.484275, -1.485866),
latitude= c(54.90083, 54.90078, 54.90077, 54.90011, 54.89936),
ID = c(LETTERS[1:5]))
# 4326 is common, but a poor choice for accurate distances. you should choose a better crs.
cen_sf <- st_as_sf(centroid, coords = c('longitude', 'latitude')) %>% st_set_crs(4326)
loc_sf <- st_as_sf(locations, coords = c('longitude', 'latitude')) %>% st_set_crs(4326)
st_nearest_feature(loc_sf, cen_sf)
#> although coordinates are longitude/latitude, st_nearest_feature assumes that they are planar
#> [1] 1 1 1 1 2
st_distance(loc_sf, cen_sf)
#> Units: [m]
#> [,1] [,2]
#> [1,] 171.1779 282.59946
#> [2,] 163.2218 270.91505
#> [3,] 165.2558 280.18680
#> [4,] 123.1550 126.21773
#> [5,] 191.5677 8.47761
Создано в 2020-01-16 с помощью пакета Представить (v0.3.0)