0

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

Inigo
  • 1
  • I'd like to try to help you answer your question. Are you able to share example data? https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Skaqqs Aug 30 '21 at 16:33

0 Answers0