如何按时间范围子集?

天生

我想从以下数据框示例创建一个子集。条件是选择那些时间列值属于从特定ID的最短时间到下一个假设一个小时的时间范围的那些行。

    id    time
    1   1468696537          
    1   1468696637          
    1   1482007490          
    2   1471902849          
    2   1471902850          
    2   1483361074          
    3   1474207754          
    3   1474207744          
    3   1471446673          
    3   1471446693  

输出应该是这样的:

   id    time
    1   1468696537          
    1   1468696637          
    2   1471902849          
    2   1471902850          
    3   1471446673          
    3   1471446693  

请帮我怎么做?

莫里斯·埃弗斯(Maurits Evers)

我们可以执行以下操作:

library(magrittr);
library(dplyr);
df %>%
    group_by(id) %>%
    filter(time <= min(time) + 3600)
#     id       time
#  <int>      <int>
#1     1 1468696537
#2     1 1468696637
#3     2 1471902849
#4     2 1471902850
#5     3 1471446673
#6     3 1471446693

说明:将条目分组为id,然后是中的filter条目min(time) + 1 hour


样本数据

df <- read.table(text =
    "   id    time
    1   1468696537
    1   1468696637
    1   1482007490
    2   1471902849
    2   1471902850
    2   1483361074
    3   1474207754
    3   1474207744
    3   1471446673
    3   1471446693  ", header = T)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章