【问题标题】:Faster way of finding distances between combinations of points查找点组合之间距离的更快方法
【发布时间】:2020-09-02 12:27:43
【问题描述】:

我有一个不同组中的点数据框。我的实际数据框长度超过一千行。对于每个组的组合,我需要找到组合中每个点与其他点之间的距离。我总结了每个点的距离。我有一个解决方案,但是当我处理 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

【问题讨论】:

标签: r loops combinations distance shapefile


【解决方案1】:

将您的数据分配给名为 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,因为它们中只有一个点。

【讨论】:

  • 不,这不是我想要的。我不是在总结整个组合,而是逐点总结。
  • 更新了我的答案。它现在会生成看起来像您的结果。我找不到你的距离函数st_distance,所以我知道我的距离与你的不匹配。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-03
  • 2019-09-16
  • 2012-06-20
  • 1970-01-01
相关资源
最近更新 更多