gpt4 book ai didi

r - 使用 sf dplyr 在 R 中按组计算逐点距离

转载 作者:行者123 更新时间:2023-12-01 08:11:10 24 4
gpt4 key购买 nike

我有 2 个数据框。如果第一帧相对于第二个数据帧中的某个点,我想计算所有 POINT 几何之间的距离。这个问题的主要特征是我在第一个dataframe中有一个grouping变量,我想根据这个分组指标选择对应的点来测量到(在第二个dataframe中)的距离.我尝试使用 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)

关于r - 使用 sf dplyr 在 R 中按组计算逐点距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54887209/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com