繁体   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