compute pointwise distance by group in R with sf dplyr

怎甘沉沦 提交于 2021-02-07 23:02:14

问题


I have 2 dataframes. I want to compute the distance between all POINT geometries if the first frame with respect to a certain POINT in the second dataframe. The main feature of this problem is that I have a grouping variable in the first dataframe, and I would like to select the corresponding point to measure the distance to (in the second dataframe) according to this grouping indicator. I tried with group_by:

library(sf)
library(dplyr)

d = data.frame(x = 1:10,y = 1:10, g = rep(c("a","b"),each=5))
d_sf = st_as_sf(d,coords = c("x","y") )
d_sf

Simple feature collection with 10 features and 1 field
geometry type:  POINT
dimension:      XY
bbox:           xmin: 1 ymin: 1 xmax: 10 ymax: 10
epsg (SRID):    NA
proj4string:    NA
   g      geometry
1  a   POINT (1 1)
2  a   POINT (2 2)
3  a   POINT (3 3)
4  a   POINT (4 4)
5  a   POINT (5 5)
6  b   POINT (6 6)
7  b   POINT (7 7)
8  b   POINT (8 8)
9  b   POINT (9 9)
10 b POINT (10 10)

centers = d %>% group_by(g) %>% summarise(x = mean(x), y = mean(y))
centers
centers_sf = st_as_sf(centers, coords = c("x","y"))
Simple feature collection with 2 features and 1 field
geometry type:  POINT
dimension:      XY
bbox:           xmin: 3 ymin: 3 xmax: 8 ymax: 8
epsg (SRID):    NA
proj4string:    NA
# A tibble: 2 x 2
  g     geometry
  <fct>  <POINT>
1 a        (3 3)
2 b        (8 8)

d_sf %>% group_by(g) %>% st_distance(centers_sf,by_element = TRUE)
 [1] 2.828427 8.485281 0.000000 5.656854 2.828427 2.828427 5.656854 0.000000 8.485281 2.828427

# but really I want this:
> st_distance(d_sf[1,],centers_sf[1,])
         [,1]
[1,] 2.828427
> st_distance(d_sf[2,],centers_sf[1,])
         [,1]
[1,] 1.414214
> st_distance(d_sf[3,],centers_sf[1,])
     [,1]
[1,]    0

回答1:


Is this what you are looking for?

library(tidyverse)

d_sf %>%
  mutate(dst = map2_dbl(g, geometry,
    ~ st_distance(.y, centers_sf %>% filter(g == .x) %>% pull(geometry))
  ))

Output:

   g      dst      geometry
1  a 2.828427   POINT (1 1)
2  a 1.414214   POINT (2 2)
3  a 0.000000   POINT (3 3)
4  a 1.414214   POINT (4 4)
5  a 2.828427   POINT (5 5)
6  b 2.828427   POINT (6 6)
7  b 1.414214   POINT (7 7)
8  b 0.000000   POINT (8 8)
9  b 1.414214   POINT (9 9)
10 b 2.828427 POINT (10 10)



回答2:


Here's a slightly modified answer that works when crs is defined:

d_sf$dst <- map_dbl(1:nrow(d_sf), function(x){
  x <- d_sf[x,]
  y <- centers_sf[centers_sf$g == x$g,]
  st_distance(x, y)
})


来源:https://stackoverflow.com/questions/54887209/compute-pointwise-distance-by-group-in-r-with-sf-dplyr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!