I have a panel dataset with thousands of cities data (inter_sf), and another spatial file containing multiple provincial borders (borders, each observation is a border).
I calculated the minimum distance from all cities to all provincial borders, and kept only the minimum distance to the closest provincial border.
I want to create a new variable identifying the ID of the provincial border to which the minimum distance is calculated from in each city.
This is my code:
inter_sf <- st_as_sf(inter, coords = c("longitud", "latitud"), crs = st_crs(provinces_sf))
i <- st_intersection(provinces_sf, provinces_sf)
borders <- i[i$GID_2 != i$GID_2.1,]
min_dist <- sapply(1:nrow(inter_sf), function(x) min(st_distance(borders, inter_sf[x, ])))
inter2_sf <- cbind(inter_sf, min_distance = min_dist)
But now I don't know how to include a new column identifying the specific provincial border that each "min_distance" corresponds to.
Thank you, Inigo