r - 计算分组数据中各点之间的欧式距离

标签 r dplyr distance sp geosphere

在下面的数据中(包含在 dput 中),我对三个人 (IndIDII) 进行了重复观察(纬度和经度)。请注意,每个人的位置数量不同。

> Dat
  IndIDII      IndYear  WintLat  WintLong
1 BHS_265 BHS_265-2015 47.61025 -112.7210
2 BHS_265 BHS_265-2016 47.59884 -112.7089
3 BHS_770 BHS_770-2016 42.97379 -109.0400
4 BHS_770 BHS_770-2017 42.97129 -109.0367
5 BHS_770 BHS_770-2018 42.97244 -109.0509
6 BHS_377 BHS_377-2015 43.34744 -109.4821
7 BHS_377 BHS_377-2016 43.35559 -109.4445
8 BHS_377 BHS_377-2017 43.35195 -109.4566
9 BHS_377 BHS_377-2018 43.34765 -109.4892

我想计算每个人的连续点之间的欧氏距离。我最初的想法是使用 lead()dplyr 中工作,如下所示。 distm 函数需要一个矩阵,我一直无法在 dplyr 中创建它。是否可以生成矩阵并将其用作 distm 的参数?

Dat %>% 
  group_by(IndIDII) %>% 
  mutate(WitnGeoDist = distm(as.matrix(c("WintLong", "WintLat")), lead(as.matrix(c("WintLong", "WintLat"))), fun = distVincentyEllipsoid))

或者,是否还有其他可能性...?提前谢谢了。

数据:

Dat <- structure(list(IndIDII = c("BHS_265", "BHS_265", "BHS_770", "BHS_770", 
"BHS_770", "BHS_377", "BHS_377", "BHS_377", "BHS_377"), IndYear = c("BHS_265-2015", 
"BHS_265-2016", "BHS_770-2016", "BHS_770-2017", "BHS_770-2018", 
"BHS_377-2015", "BHS_377-2016", "BHS_377-2017", "BHS_377-2018"
), WintLat = c(47.6102519805014, 47.5988417247191, 42.9737859090909, 
42.9712914772727, 42.9724390816327, 43.3474354347826, 43.3555934579439, 
43.3519543396226, 43.3476466990291), WintLong = c(-112.720994832869, 
-112.708887595506, -109.039964727273, -109.036693522727, -109.050923061224, 
-109.482114456522, -109.444522149533, -109.45659254717, -109.489241553398
)), class = "data.frame", row.names = c(NA, -9L))

最佳答案

这是一种更好地利用 group_by 并通过使用 purrr::possibly 使 geosphere::distm 工作的不同方法。这让我们可以为距离没有意义的行填写 NA,因为没有以前的值可以使用。

Dat <- structure(list(IndIDII = c("BHS_265", "BHS_265", "BHS_770", "BHS_770", "BHS_770", "BHS_377", "BHS_377", "BHS_377", "BHS_377"), IndYear = c("BHS_265-2015", "BHS_265-2016", "BHS_770-2016", "BHS_770-2017", "BHS_770-2018", "BHS_377-2015", "BHS_377-2016", "BHS_377-2017", "BHS_377-2018"), WintLat = c(47.6102519805014, 47.5988417247191, 42.9737859090909, 42.9712914772727, 42.9724390816327, 43.3474354347826, 43.3555934579439, 43.3519543396226, 43.3476466990291), WintLong = c(-112.720994832869, -112.708887595506, -109.039964727273, -109.036693522727, -109.050923061224, -109.482114456522, -109.444522149533, -109.45659254717, -109.489241553398)), class = "data.frame", row.names = c(NA, -9L))
library(tidyverse)
poss_dist <- possibly(geosphere::distm, otherwise = NA)
Dat %>%
  nest(WintLong, WintLat, .key = "coords") %>%
  group_by(IndIDII) %>%
  mutate(prev_coords = lag(coords)) %>%
  ungroup() %>%
  mutate(WitnGeoDist = map2_dbl(coords, prev_coords, poss_dist))
#> # A tibble: 9 x 5
#>   IndIDII IndYear      coords              prev_coords         WitnGeoDist
#>   <chr>   <chr>        <list>              <list>                    <dbl>
#> 1 BHS_265 BHS_265-2015 <data.frame [1 x 2~ <lgl [1]>                   NA 
#> 2 BHS_265 BHS_265-2016 <data.frame [1 x 2~ <data.frame [1 x 2~       1561.
#> 3 BHS_770 BHS_770-2016 <data.frame [1 x 2~ <lgl [1]>                   NA 
#> 4 BHS_770 BHS_770-2017 <data.frame [1 x 2~ <data.frame [1 x 2~        385.
#> 5 BHS_770 BHS_770-2018 <data.frame [1 x 2~ <data.frame [1 x 2~       1168.
#> 6 BHS_377 BHS_377-2015 <data.frame [1 x 2~ <lgl [1]>                   NA 
#> 7 BHS_377 BHS_377-2016 <data.frame [1 x 2~ <data.frame [1 x 2~       3180.
#> 8 BHS_377 BHS_377-2017 <data.frame [1 x 2~ <data.frame [1 x 2~       1059.
#> 9 BHS_377 BHS_377-2018 <data.frame [1 x 2~ <data.frame [1 x 2~       2690.

reprex package (v0.2.0) 于 2018-09-19 创建。

关于r - 计算分组数据中各点之间的欧式距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52414879/

相关文章:

r - 长格式 tsibble 中的多个时间序列

在构建 R 包时运行预构建脚本

r - 带公式的函数的正确 dplyr 函数式编程语法是什么

r - 聚类中的大距离矩阵

python - 3维空间中点到线的最短距离

r - 你能有一个图像作为 Shiny 的单选按钮选择吗?

oop - 在 R 的 S4 中定义具有多个可选空插槽的类的有效方法?

r - 在 dplyr 中使用过滤器内的过滤器会产生意想不到的结果

r - 将多个值传播到R中数据框中的唯一值

R : How to use apply in this case to speed up the function?