简体   繁体   中英

Distance to nearest point by group using sf

I have a dataset that looks similar to the example below. For each code I would like to calculate the distance to the next nearest code that belongs to the same area as it. So in my example, for each code belonging to area A001 I would be after an additional column in the dataset that contains the minimum distance to one of the other points that belong to area A001. I assume there should be a way of using st_distance to achieve this?

require("data.table")
require("sf")

dt1 <- data.table(
code=c("A00111", "A00112","A00113","A00211","A00212","A00213","A00214","A00311","A00312"),
area=c("A001", "A001","A001","A002","A002","A002","A002","A003","A003"),
x=c(325147,323095,596020,257409,241206,248371,261076,595218,596678),
y=c(286151,284740,335814,079727,084266,078283,062045,333889,337836))

sf1 <- st_as_sf(dt1, coords = c("x","y"), crs=27700, na.fail=FALSE)

There might be a 'cleaner' way to get here, but this gets you the correct values.

library(tidyverse)

# intermediate fun to help later in apply()
smallest_non_zero <- function(x) {
  min_val <- min(x[x != 0])
  x[match(min_val, x)]
}

closest_grp_distances <- sf1 %>%
  group_split(area) %>%
  map(~st_distance(., .) %>% # returns matrix
       apply(1, smallest_non_zero)) %>%
  unlist()

sf1$closest_grp_distances <- closest_grp_distances

I wanted to use the baseR split but it doesn't have a method for sf objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM