使用 sf dplyr 在 R 中按组计算逐点距离 [英] compute pointwise distance by group in R with sf dplyr
本文介绍了使用 sf dplyr 在 R 中按组计算逐点距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 2 个数据框.如果第一帧相对于第二个数据帧中的某个点,我想计算所有 POINT 几何图形之间的距离.这个问题的主要特点是我在第一个数据框中有一个分组变量,我想根据这个分组指标选择对应的点来测量到(在第二个数据框中)的距离.我用 group_by
试过:
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
推荐答案
这是您要找的吗?
library(tidyverse)
d_sf %>%
mutate(dst = map2_dbl(g, geometry,
~ st_distance(.y, centers_sf %>% filter(g == .x) %>% pull(geometry))
))
输出:
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)
这篇关于使用 sf dplyr 在 R 中按组计算逐点距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文