簡體   English   中英

更快地找到點組合之間的距離

[英]Faster way of finding distances between combinations of points

我有一個不同組中點的數據框。 我的實際數據幀超過一千行。 對於每個組的組合,我需要找到組合中每個點與其他點之間的距離。 我總結了每個點的距離。 我有一個解決方案,但是當我處理 63 種組合時它很慢。

為了說明我當前的解決方案,請考慮我只有三個組的示例。 我將它們分類為所有可能的組合,即組合 1 僅包含第 1 組,組合 4 包含第 1 組和第 2 組....(以下可重現數據)

然后我將我的數據框轉換為點的 shapefile:

points <- points_csv %>%st_as_sf(coords = c('longitude', 'latitude'))

然后我制作了一個不同組合的向量:

Combination_list = points$combination
Combination_list <- unique(Combination_list)

並使用以下循環:

Density_total = data.frame()
for (b in Combination_list){

filtered <- filter(points, combination == b)

x <- filtered$geometry

for (t in filtered$geometry){
test_point <- filtered$geometry[t]
M <- st_distance(test_point,x)
M <- unclass(M)

D <- sum(M)

df1 <- data.frame(D)

Density_total <- rbind(Density_total,df1)
}}

可重現的數據:

structure(list(Name = c("Group1", "Group1", "Group2", "Group3", 
"Group1", "Group1", "Group2", "Group1", "Group1", "Group3", "Group2", 
"Group3", "Group1", "Group2", "Group3"), combination = c("Combination1", 
"Combination1", "Combination2", "Combination3", "Combination4", 
"Combination4", "Combination4", "Combination5", "Combination5", 
"Combination5", "Combination6", "Combination6", "Combination7", 
"Combination7", "Combination7"), latitude = c(0.1989, 0.1989, 
0.201, 0.201, 0.1989, 0.1989, 0.201, 0.1989, 0.1989, 0.201, 0.201, 
0.201, 0.1989, 0.201, 0.201), longitude = c(-0.001, -0.0015, 
-0.0015, -0.001, -0.001, -0.0015, -0.0015, -0.001, -0.0015, -0.001, 
-0.0015, -0.001, -0.0015, -0.0015, -0.001)), class = "data.frame", row.names = c(NA, 
-15L), spec = structure(list(cols = list(Name = structure(list(), class = 
c("collector_character", 
"collector")), combination = structure(list(), class = c("collector_character", 
"collector")), latitude = structure(list(), class = c("collector_double", 
"collector")), longitude = structure(list(), class = c("collector_double", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), skip = 1), class = "col_spec"))

所需的輸出應如下所示:

Distance      X       Y    Combination
0.000500000 0.1989 -0.0010 Combination1
0.000500000 0.1989 -0.0015 Combination1
0.000000000 0.2010 -0.0015 Combination2
0.000000000 0.2010 -0.0010 Combination3
0.002658703 0.1989 -0.0010 Combination4
0.002600000 0.1989 -0.0015 Combination4
0.004258703 0.2010 -0.0015 Combination4
0.002600000 0.1989 -0.0010 Combination5
0.002658703 0.1989 -0.0015 Combination5
0.004258703 0.2010 -0.0010 Combination5
0.000500000 0.2010 -0.0015 Combination6
0.000500000 0.2010 -0.0010 Combination6
0.004758703 0.1989 -0.0010 Combination7
0.004758703 0.1989 -0.0015 Combination7
0.004758703 0.2010 -0.0015 Combination7
0.004758703 0.2010 -0.0010 Combination7

將您的數據分配給名為points的 data.frame。 這是一個dplyr方法來做到這一點。 您可以使用full_join生成所有組合,然后計算距離。 在我的機器上使用您的示例數據不到一秒鍾。

library(dplyr)
points %>% 
  full_join(points, by = c("combination" = "combination")) %>%
  mutate(distance = (longitude.x - longitude.y)^2 + (latitude.x - latitude.y)^2) %>%
  group_by(latitude.x, longitude.x, combination) %>%
  summarise(total = sum(distance)) %>%
  select(Distance = total, X = latitude.x, Y = longitude.x, combination) %>% 
  arrange(combination)
`summarise()` regrouping output by 'latitude.x', 'longitude.x' (override with `.groups` argument)

# A tibble: 15 x 4
# Groups:   X, Y [4]
     Distance     X       Y combination 
        <dbl> <dbl>   <dbl> <chr>       
 1 0.00000025 0.199 -0.0015 Combination1
 2 0.00000025 0.199 -0.001  Combination1
 3 0          0.201 -0.0015 Combination2
 4 0          0.201 -0.001  Combination3
 5 0.00000466 0.199 -0.0015 Combination4
 6 0.00000491 0.199 -0.001  Combination4
 7 0.00000907 0.201 -0.0015 Combination4
 8 0.00000491 0.199 -0.0015 Combination5
 9 0.00000466 0.199 -0.001  Combination5
10 0.00000907 0.201 -0.001  Combination5
11 0.00000025 0.201 -0.0015 Combination6
12 0.00000025 0.201 -0.001  Combination6
13 0.00000907 0.199 -0.0015 Combination7
14 0.00000466 0.201 -0.0015 Combination7
15 0.00000491 0.201 -0.001  Combination7

在這個樣本集中,組合 2 和 3 的總距離為 0,因為它們中只有一個點。

暫無
暫無

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

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