【问题标题】:Create new dataframe with one row for each pair of rows with consecutive sequence in existing dataframe为现有数据框中具有连续序列的每对行创建一行新数据框
【发布时间】:2021-04-09 07:48:35
【问题描述】:

我有一个现有的数据框,其中每一行代表一个地理点。每个点都由一个唯一的 ID、一个用户定义的序列号及其一对地理坐标定义,如下所示:

id  Sequence Latitude Longitude Trajectory
544        0 41.37990   2.17220          1
545        1 41.37874   2.17104          1
546        0 41.37867   2.17092          2
547        1 41.37863   2.17084          2
548        2 41.37857   2.17073          2
549        3 41.37853   2.17065          2

请注意,这些点来自一系列轨迹,其中每个轨迹由序列变量后面的连续点形成。我还有一个现有的变量“轨迹”对这些连续点进行分组。所以,在这个例子中,有一个由两个点组成的轨迹,然后是一个由 4 个点组成的轨迹。

我需要创建一个新的数据框(我们称之为“线”),其中每条线需要一行,连接同一轨迹中的两个连续点。每行需要包含两个点索引及其两对坐标,理想情况下,还需要包含轨迹编号。所以前面例子的结果是:

Line  id1 Latitude1 Longitude1 id2 Latitude2 Longitude2 Trajectory
0     544  41.37990    2.17220 545  41.37874    2.17104          1
1     546  41.37867    2.17092 547  41.37863    2.17084          2
2     547  41.37863    2.17084 548  41.37857    2.17073          2
3     548  41.37857    2.17073 549  41.37853    2.17065          2

我一直在尝试使用 tidyverse、dplyr 和类似的库来避免使用 for 循环,因为我知道它们效率不高,并且现有的数据框有数百万个点,但没有任何效果,我找不到任何类似的问题。

欢迎任何有关如何解决该问题的帮助。提前谢谢!

【问题讨论】:

    标签: r dataframe sequence point


    【解决方案1】:

    当原始数据按轨迹和序列排序时,我可以在经度、纬度和轨迹列中移动一行,使经度为 1/2,纬度为 1/2,轨迹为 1/2,然后提取行轨迹相同。

    假设原始数据框是“点”:

    n = nrow(points)
    temp <- data.frame(
      id1 = points$id[1:(n-1)],
      Latitude1 = points$Latitude[1:(n-1)],
      Longitude1 = points$Longitude[1:(n-1)],
      id2 = points$id[2:n],
      Latitude2 = points$Latitude[2:n],
      Longitude2 = points$Longitude[2:n],
      Trajectory1 = points$Trajectory[1:(n-1)],
      Trajectory2 = points$Trajectory[2:n]
    )
    temp=temp[Trajectory1==Trajectory2,]
    n = nrow(temp)
    ret <- data.frame(
      Line = c(0:(n-1)),
      id1 = temp$id1,
      Latitude1 = temp$Latitude1,
      Longitude1 = temp$Longitude1,
      id2 = temp$id2,
      Latitude2 = temp$Latitude2,
      Longitude2 = temp$Longitude2,
      Trajectory = temp$Trajectory1
    )
    

    ret 是输出。

    【讨论】:

    • 很好的观察!没想过换一排,好主意。它就像一个魅力,比@det approach 快得多(不过还是谢谢!)。我也很欣赏它使用基础 R 并且它是如此清晰易懂。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 2023-02-10
    • 2021-06-16
    • 1970-01-01
    • 2021-09-08
    • 2021-06-10
    相关资源
    最近更新 更多