首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在r中按时差对数据进行分组?

如何在r中按时差对数据进行分组?
EN

Stack Overflow用户
提问于 2020-08-10 00:23:54
回答 1查看 78关注 0票数 0

我想根据posxict列对数据集进行分组。我需要分组,与以前的观察相比,观测之间的时间差不超过3小时。这是我的数据样本:

代码语言:javascript
复制
time                type      day month gas           response
   <dttm>              <chr>   <int> <dbl> <chr>            <dbl>
 1 2018-08-21 14:30:00 old_std    21     8 benzene_area 10260721 
 2 2018-08-21 15:30:00 old_std    21     8 benzene_area  2591905 
 3 2018-11-09 20:00:00 old_std     9    11 benzene_area   684166
 4 2018-11-09 23:00:00 old_std     9    11 benzene_area   589498
 5 2018-11-10 02:00:00 old_std    10    11 benzene_area   460714
 6 2018-11-10 05:00:00 old_std    10    11 benzene_area   490663
 7 2018-11-10 11:30:00 old_std    10    11 benzene_area   404947
 8 2018-11-10 14:30:00 old_std    10    11 benzene_area   402566
 9 2018-11-10 16:30:00 old_std    10    11 benzene_area   362376
10 2018-11-11 00:00:00 old_std    11    11 benzene_area   276184

这就是我要找的:

代码语言:javascript
复制
time                type      day month gas           response   group
   <dttm>              <chr>   <int> <dbl> <chr>            <dbl>
 1 2018-08-21 14:30:00 old_std    21     8 benzene_area 10260721   1
 2 2018-08-21 15:30:00 old_std    21     8 benzene_area  2591905   1
 3 2018-11-09 20:00:00 old_std     9    11 benzene_area   684166   2
 4 2018-11-09 23:00:00 old_std     9    11 benzene_area   589498   2
 5 2018-11-10 02:00:00 old_std    10    11 benzene_area   460714   2
 6 2018-11-10 05:00:00 old_std    10    11 benzene_area   490663   2
 7 2018-11-10 11:30:00 old_std    10    11 benzene_area   404947   3
 8 2018-11-10 14:30:00 old_std    10    11 benzene_area   402566   3
 9 2018-11-10 16:30:00 old_std    10    11 benzene_area   362376   3
10 2018-11-11 00:00:00 old_std    11    11 benzene_area   276184   4

我还没有找到像这样分组数据的方法。有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-10 00:31:29

您可以使用lag获取以前的值,并在difftime中使用它来获得小时内的差异,并在每次差值大于3小时时增加组值。

代码语言:javascript
复制
library(dplyr)
df %>%
        mutate(group = cumsum(difftime(time, lag(time, default = first(time)), 
                              units = "hours") > 3) + 1)

#                  time    type day month          gas response group
#1  2018-08-21 14:30:00 old_std  21     8 benzene_area 10260721     1
#2  2018-08-21 15:30:00 old_std  21     8 benzene_area  2591905     1
#3  2018-11-09 20:00:00 old_std   9    11 benzene_area   684166     2
#4  2018-11-09 23:00:00 old_std   9    11 benzene_area   589498     2
#5  2018-11-10 02:00:00 old_std  10    11 benzene_area   460714     2
#6  2018-11-10 05:00:00 old_std  10    11 benzene_area   490663     2
#7  2018-11-10 11:30:00 old_std  10    11 benzene_area   404947     3
#8  2018-11-10 14:30:00 old_std  10    11 benzene_area   402566     3
#9  2018-11-10 16:30:00 old_std  10    11 benzene_area   362376     3
#10 2018-11-11 00:00:00 old_std  11    11 benzene_area   276184     4

在R基地,这将是:

代码语言:javascript
复制
df$group <- cumsum(c(TRUE, difftime(df$time[-1], df$time[-nrow(df)],
                    units = "hours") > 3))
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63332635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档