根据 R 中的时间间隔对数据进行分组并分配组 ID

Posted

技术标签:

【中文标题】根据 R 中的时间间隔对数据进行分组并分配组 ID【英文标题】:Group data and assign group id based on time intervals in R 【发布时间】:2022-01-17 01:34:21 【问题描述】:

我试图弄清楚如何根据 R 中的时间间隔分配组 ID。

更多背景信息:我已将 GPS 数据(纬度/经度数据点,以不规则间隔记录)与加速度数据(ACC“突发”82 个数据点,在每分钟开始时记录 - 所有 82 个数据点合二为一) Burst 具有相同的时间戳)。

由于 GPS 点和 ACC 突发是同时收集的,我现在想将 GPS 点与相关的 ACC 突发分组:分配在同一分钟内发生的所有 GPS 和 ACC 数据,一个唯一的组 ID .

编辑:这里有一些示例数据。我想在同一分钟内将第 8 行中的 GPS 点分组到 ACC 数据(在本例中为 GPS 点上方)。

structure(list(X.1 = 1:11, timestamp = c("2019-01-26T16:25:00Z", "2019-01-26T16:25:00Z", "2019-01-26T16:25:00Z", "2019-01-26T16:25:00Z", "2019-01-26T16:25:00Z", "2019-01-26T16:25:00Z", "2019-01-26T16:25:00Z", "2019-01-26T16:25:47Z", "2019-01-26T16:26:00Z", "2019-01-26T16:26:00Z", "2019-01-26T16:26:00Z"), sensor.type = c("acceleration", "acceleration", "acceleration", "acceleration", "acceleration", "acceleration", "acceleration", "gps", "acceleration", "acceleration", "acceleration"), location.long = c(NA, NA, NA, NA, NA, NA, NA, 44.4777343, NA, NA, NA), location.lat = c(NA, NA, NA, NA, NA, NA, NA, -12.2839707, NA, NA, NA), annotation = c("Moving/Climbing", "Moving/Climbing", "Moving/Climbing", "Moving/Climbing", "Moving/Climbing", "Moving/Climbing", "Moving/Climbing", "Moving/Climbing", "Moving/Climbing", "Moving/Climbing", "Moving/Climbing"), X = c(2219L, 1694L, 1976L, 1744L, 2014L, 2202L, 2269L, NA, 1874L, 2024L, 1990L), Y = c(1416L, 1581L, 1524L, 1620L, 1409L, 1545L, 1771L, NA, 1687L, 1773L, 1813L), Z = c(2189L, 2209L, 2121L, 2278L, 2003L, 2034L, 2060L, NA, 2431L, 2504L, 2428L)), class = "data.frame", row.names = c(NA, -11L))

X.1            timestamp    sensor.type     location.long   location.lat annotation   X    Y    Z
1    1 2019-01-26T16:25:00Z acceleration            NA           NA Moving/Climbing 2219 1416 2189        
2    2 2019-01-26T16:25:00Z acceleration            NA           NA Moving/Climbing 1694 1581 2209       
3    3 2019-01-26T16:25:00Z acceleration            NA           NA Moving/Climbing 1976 1524 2121       
4    4 2019-01-26T16:25:00Z acceleration            NA           NA Moving/Climbing 1744 1620 2278       
5    5 2019-01-26T16:25:00Z acceleration            NA           NA Moving/Climbing 2014 1409 2003        
6    6 2019-01-26T16:25:00Z acceleration            NA           NA Moving/Climbing 2202 1545 2034       
7    7 2019-01-26T16:25:00Z acceleration            NA           NA Moving/Climbing 2269 1771 2060       
8    8 2019-01-26T16:25:47Z gps               44.47773    -12.28397 Moving/Climbing   NA   NA   NA
9    9 2019-01-26T16:26:00Z acceleration            NA           NA Moving/Climbing 1874 1687 2431        
10  10 2019-01-26T16:26:00Z acceleration            NA           NA Moving/Climbing 2024 1773 2504       
11  11 2019-01-26T16:26:00Z acceleration            NA           NA Moving/Climbing 1990 1813 2428        


   

这有意义吗?我知道 lubridate 可以根据时间间隔汇总数据,但是如何根据时间戳添加新的组 ID(变量)?

【问题讨论】:

请以复制/粘贴格式分享一些示例数据。 dput(your_data[1:10, ]) 非常适合前 10 行。选择一个合适的小子集来说明问题。 谢谢,我添加了一些示例数据! 【参考方案1】:

这是使用dplyrlubridate 的解决方案。我们将您的 timestamp 列转换为适当的日期时间类,添加一个向下舍入到最接近分钟的新列,然后根据四舍五入的时间戳创建一个 ID:

library(dplyr)
library(lubridate)

dat %>% 
  mutate(
    timestamp = ymd_hms(timestamp),
    minute = floor_date(timestamp, unit = "minute"),
    group_id = as.integer(factor(minute))
  )
  
#    X.1           timestamp  sensor.type location.long location.lat      annotation    X    Y    Z
# 1    1 2019-01-26 16:25:00 acceleration            NA           NA Moving/Climbing 2219 1416 2189
# 2    2 2019-01-26 16:25:00 acceleration            NA           NA Moving/Climbing 1694 1581 2209
# 3    3 2019-01-26 16:25:00 acceleration            NA           NA Moving/Climbing 1976 1524 2121
# 4    4 2019-01-26 16:25:00 acceleration            NA           NA Moving/Climbing 1744 1620 2278
# 5    5 2019-01-26 16:25:00 acceleration            NA           NA Moving/Climbing 2014 1409 2003
# 6    6 2019-01-26 16:25:00 acceleration            NA           NA Moving/Climbing 2202 1545 2034
# 7    7 2019-01-26 16:25:00 acceleration            NA           NA Moving/Climbing 2269 1771 2060
# 8    8 2019-01-26 16:25:47          gps      44.47773    -12.28397 Moving/Climbing   NA   NA   NA
# 9    9 2019-01-26 16:26:00 acceleration            NA           NA Moving/Climbing 1874 1687 2431
# 10  10 2019-01-26 16:26:00 acceleration            NA           NA Moving/Climbing 2024 1773 2504
# 11  11 2019-01-26 16:26:00 acceleration            NA           NA Moving/Climbing 1990 1813 2428
#                 minute group_id
# 1  2019-01-26 16:25:00        1
# 2  2019-01-26 16:25:00        1
# 3  2019-01-26 16:25:00        1
# 4  2019-01-26 16:25:00        1
# 5  2019-01-26 16:25:00        1
# 6  2019-01-26 16:25:00        1
# 7  2019-01-26 16:25:00        1
# 8  2019-01-26 16:25:00        1
# 9  2019-01-26 16:26:00        2
# 10 2019-01-26 16:26:00        2
# 11 2019-01-26 16:26:00        2

【讨论】:

@IsMa 很高兴听到这个消息!在这种情况下,请点击投票按钮旁边左边空白处的复选标记“接受”答案。

以上是关于根据 R 中的时间间隔对数据进行分组并分配组 ID的主要内容,如果未能解决你的问题,请参考以下文章

根据组和最大总和分配组

根据 Pandas 中的组大小对分组数据进行排序

我们如何在 AWS Neptune 上的 RDF 图中对对象进行分组并为组分配通用颜色?

以不带 0 的间隔对数据进行分组

根据 r 中的其他列对一个列表中的一列的所有值进行分组

如何根据熊猫时间序列中的 5 分钟间隔创建组 ID?