【问题标题】:Ordering data set per group, sub-group and timestamp with small offset within groups按组、子组和时间戳排序数据集,组内偏移量较小
【发布时间】:2017-10-25 15:37:41
【问题描述】:

我有一个包含 3 列的数据框 1)时间戳,2)组 3)组(子组)内的索引。一个组有 6 行/索引,并且应该始终具有相同的时间戳,最大允许偏差为 2 秒。 有时来自两个不同组的某些元素具有相同的时间戳,但其他元素则没有。我需要能够根据时间戳排列数据以对组进行聚类,但首先考虑到组中的元素可能具有最大 2 秒的偏移量。

df1 <- data.frame(
   timestamp1 = as.POSIXct(c(
      '2017-09-07 15:16:27',  '2017-09-07 15:16:27',  '2017-09-07 15:16:27',  '2017-09-07 15:16:27',  '2017-09-07 15:16:27',  '2017-09-07 15:16:27',
      '2017-09-07 15:17:19', '2017-09-07 15:17:19', '2017-09-07 15:17:19', '2017-09-07 15:17:19', 
      '2017-09-07 15:17:19', '2017-09-07 15:17:19', '2017-09-07 15:17:19', '2017-09-07 15:17:19', '2017-09-07 15:17:19', 
      '2017-09-07 15:17:20', '2017-09-07 15:17:20',
      '2017-09-07 15:17:20'
      )), 
   group = c(
      'aaa', 'aaa', 'aaa', 'aaa', 'aaa', 'aaa', 
      'a', 'a', 'a', 'a',
      'aaa', 'aaa', 'aaa', 'aaa', 'aaa',
      'a', 'a',
      'aaa'
      ),
   index_inside_group = c(
      1, 2, 3, 4, 5, 6,
      1, 3, 4, 6,
      1, 2, 4, 5, 6,
      2, 5,
      3
      )
   )
> df1
            timestamp1 group index_inside_group
1  2017-09-07 15:16:27   aaa                  1
2  2017-09-07 15:16:27   aaa                  2
3  2017-09-07 15:16:27   aaa                  3
4  2017-09-07 15:16:27   aaa                  4
5  2017-09-07 15:16:27   aaa                  5
6  2017-09-07 15:16:27   aaa                  6
7  2017-09-07 15:17:19     a                  1
8  2017-09-07 15:17:19     a                  3
9  2017-09-07 15:17:19     a                  4
10 2017-09-07 15:17:19     a                  6
11 2017-09-07 15:17:19   aaa                  1
12 2017-09-07 15:17:19   aaa                  2
13 2017-09-07 15:17:19   aaa                  4
14 2017-09-07 15:17:19   aaa                  5
15 2017-09-07 15:17:19   aaa                  6
16 2017-09-07 15:17:20     a                  2
17 2017-09-07 15:17:20     a                  5
18 2017-09-07 15:17:20   aaa                  3

简而言之,我需要从数据df1df2

> df2
            timestamp1 group index_inside_group
1  2017-09-07 15:16:27   aaa                  1
2  2017-09-07 15:16:27   aaa                  2
3  2017-09-07 15:16:27   aaa                  3
4  2017-09-07 15:16:27   aaa                  4
5  2017-09-07 15:16:27   aaa                  5
6  2017-09-07 15:16:27   aaa                  6
7  2017-09-07 15:17:19     a                  1
8  2017-09-07 15:17:20     a                  2
9  2017-09-07 15:17:19     a                  3
10 2017-09-07 15:17:19     a                  4
11 2017-09-07 15:17:20     a                  5
12 2017-09-07 15:17:19     a                  6
13 2017-09-07 15:17:19   aaa                  1
14 2017-09-07 15:17:19   aaa                  2
15 2017-09-07 15:17:20   aaa                  3
16 2017-09-07 15:17:19   aaa                  4
17 2017-09-07 15:17:19   aaa                  5
18 2017-09-07 15:17:19   aaa                  6

df2 中,数据按group 的优先级排列,然后是index_inside_group,仅在最后timestamp1 进行排列

【问题讨论】:

    标签: r


    【解决方案1】:

    我想我找到了一个很长的解决方案,方法是遍历每个唯一的时间戳,创建一个 +/- 2 秒的新时间戳范围,然后获取每个组属于该时间范围的所有值。 在每次迭代中,数据块按index_inside_group 排序,然后才附加以以正确的顺序重新创建new_df

    new_df <- data.frame()
    timestamps <- unique(df1$timestamp1)
    
    for (i in 1:length(timestamps)){
    
       thisLL <- timestamps[i] - 2 # extend lower timestamp range by 2 seconds
       thisUL <- timestamps[i] + 2 # extend upper timestamp range by 2 seconds
    
    
       group_a <- df1[df1$timestamp1 >= thisLL & df1$timestamp1 <= thisUL & df1$group == 'a',] # search for timestamps in the ranges of first group
       group_a <- group_a[order(group_a$index_inside_group), ]  # ordering subset of first group
    
       group_aaa <- df1[df1$timestamp1 >= thisLL & df1$timestamp1 <= thisUL & df1$group == 'aaa',] # search for timestamps in the ranges of second group
       group_aaa <- group_aaa[order(group_aaa$index_inside_group), ] # ordering subset of second group
    
    
       new_df <- rbind(new_df, group_a, group_aaa)
    
    }
    
    new_df <- new_df[!duplicated(new_df[])] # having +/- 2 sec timestamp ranges for each unique timestamp means we can capture duplicates of 
                                            # the same data of one group, this removes last duplicates, just in case they happen
    
    > new_df
                 timestamp1 group index_inside_group
    1   2017-09-07 15:16:27   aaa                  1
    2   2017-09-07 15:16:27   aaa                  2
    3   2017-09-07 15:16:27   aaa                  3
    4   2017-09-07 15:16:27   aaa                  4
    5   2017-09-07 15:16:27   aaa                  5
    6   2017-09-07 15:16:27   aaa                  6
    7   2017-09-07 15:17:19     a                  1
    16  2017-09-07 15:17:20     a                  2
    8   2017-09-07 15:17:19     a                  3
    9   2017-09-07 15:17:19     a                  4
    17  2017-09-07 15:17:20     a                  5
    10  2017-09-07 15:17:19     a                  6
    11  2017-09-07 15:17:19   aaa                  1
    12  2017-09-07 15:17:19   aaa                  2
    18  2017-09-07 15:17:20   aaa                  3
    13  2017-09-07 15:17:19   aaa                  4
    14  2017-09-07 15:17:19   aaa                  5
    15  2017-09-07 15:17:19   aaa                  6
    71  2017-09-07 15:17:19     a                  1
    161 2017-09-07 15:17:20     a                  2
    81  2017-09-07 15:17:19     a                  3
    91  2017-09-07 15:17:19     a                  4
    171 2017-09-07 15:17:20     a                  5
    101 2017-09-07 15:17:19     a                  6
    111 2017-09-07 15:17:19   aaa                  1
    121 2017-09-07 15:17:19   aaa                  2
    181 2017-09-07 15:17:20   aaa                  3
    131 2017-09-07 15:17:19   aaa                  4
    141 2017-09-07 15:17:19   aaa                  5
    151 2017-09-07 15:17:19   aaa                  6
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-03
      • 2018-12-17
      • 2011-09-23
      • 2016-06-29
      相关资源
      最近更新 更多