簡體   English   中英

使用sf dplyr在R中按組計算點向距離

[英]compute pointwise distance by group in R with sf dplyr

我有2個數據框。 如果第一幀相對於第二個數據幀中的某個POINT,我想計算所有POINT幾何之間的距離。 這個問題的主要特征是,我在第一個數據幀中有一個分組變量,並且我想根據該分組指示符選擇相應的點以測量到(在第二個數據幀中)的距離。 我嘗試了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)

這是定義了crs時可以稍作修改的答案:

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)
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM