R;滚动窗口 : calculate bearing of 3 previos points and compare it to bearing of following 3 values in a dataframe

标签 r dplyr data.table tidyr sf

我想解决的总体任务:使用移位函数我想计算平均方位 geosphere::bearing(p1, p2,a=6378137, f=1/298.257223563) previos 3 点(滞后)并将其与以下 3 点(领先)的方位进行比较。

这意味着

  1. 计算所有 3 个点之间的方位(滞后)
mean(bearing(point1,point2),bearing(point1,point3),bearing(point2,point3))

以及接下来的 3 分之间(领先)。

mean(bearing(point4,point5),bearing(point4,point6)bearing(point5,point6))
  1. 计算这些轴承的平均值
  2. 如果前 3 个(滞后)点的平均方位与接下来的 3 个(领先)点不同(basciclaiy abs(dif)),则丢弃(领先)3 个点。

这样做的最佳方法是什么? 它不一定是移位函数,但我认为它可能是合适的。 我只是不想写循环。 这是一个示例路径:

path<-structure(list(counter = 1:24, lon = c(11.83000844, 11.82986091, 
11.82975536, 11.82968137, 11.82966589, 11.83364579, 11.83346388, 
11.83479848, 11.83630055, 11.84026754, 11.84215965, 11.84530872, 
11.85369492, 11.85449806, 11.85479096, 11.85888555, 11.85908087, 
11.86262424, 11.86715538, 11.86814045, 11.86844252, 11.87138302, 
11.87579809, 11.87736704), lat = c(48.10980039, 48.10954023, 
48.10927434, 48.10891122, 48.10873965, 48.09824039, 48.09526792, 
48.0940306, 48.09328273, 48.09161348, 48.09097173, 48.08975325, 
48.08619985, 48.08594538, 48.08576984, 48.08370241, 48.08237208, 
48.08128785, 48.08204915, 48.08193609, 48.08186387, 48.08102563, 
48.07902278, 48.07827614)), row.names = c(NA, -24L), class = c("data.table", 
"data.frame"))

谢谢。

最佳答案

我不相信这对解决另一个问题 (R: Detect a "main" Path and remove or filter the GPS trace maybe using a kernel?) 有帮助,但这是一种计算以下两点平均值的方法。

首先你 expand_grid 得到所有对,然后你过滤到你感兴趣的对。然后你创建一个新的数据框进一步过滤,这样对于每个计数器你有三个轴承,此时您可以取平均值。

首先:我想让每个纬度/经度对与其他每个纬度/经度对匹配。为此,我使用 expand_grid,并希望通过自身扩展我们的数据。这从表面上看是失败的,因为您需要为 expand_grid 的每个参数指定唯一的名称。因此,我在调用之前 setNames

然后:我们只需要这些点对的一个子集。特别是,我们想要 counter_2 小于 counter + 2 的任何情况(例如,您想要 counter = 1counter_2 %in% c(2,3) , counter = 2counter_2 %in% c(3,4)...)

然后您需要按行遍历数据集并计算每一行的方位。我们称此数据框为 data_tmp

然后我们进行映射和过滤,以获取每个counter 值所需的行。

library(tidyverse)

data_tmp <- path %>% 
  as_tibble() %>% 
  (function(X)expand_grid(X, 
                          X %>% setNames(c("counter_2", "lon_2", "lat_2")))) %>% 
  filter(counter_2 <= counter + 2 & counter_2 > counter) %>%
  rowwise() %>%
  mutate(bearing = geosphere::bearing(c(lon, lat), c(lon_2,lat_2))) %>%
  ungroup()

three_grouped <- tibble(counter = 1:max(path$counter)) %>%
  mutate(dataz = map(.x = counter, ~ data_tmp %>% 
                      slice(which(data_tmp$counter_2 %in% 
                                    data_tmp$counter_2[data_tmp$counter == .x] &
                                    data_tmp$counter <= .x + 1 &
                                    data_tmp$counter >= .x)))) 

three_grouped %>%
  mutate(average_bearing = map_dbl(dataz, ~ mean(.x$bearing)))

关于R;滚动窗口 : calculate bearing of 3 previos points and compare it to bearing of following 3 values in a dataframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64210625/

相关文章:

r - 将连续 NA 减少到单个 NA 的最佳方法

R - 子集类别

r - dplyr 小组评估,同时评估单个项目

r - 将两个 sampleID 的相应值连接到一个新的单列中

r - 对于每个 `pop` 获取 `id` 元素的频率

r - ggplot,方面,饼图 : placing text in the middle of pie chart slices

r - 如何在R中将 block 组数据保存为文本格式?

r - R 中数据帧的智能转置

r - data.table 内/外连接与 NA 在双 bug 类型的连接列中?

r - 是否可以就地(破坏性地)修改data.frame?