使用带有时间戳的 运行 长度编码

Using run-length encoding with timestamps

我的 objective 是使用 rle() 函数来找出自行车站空了多长时间。使用下面的 test 数据,rle(test$bikes) 将 return test$bikes.

中重复值的长度
> rle(test$bikes)
Run Length Encoding
  lengths: int [1:9] 3 2 3 1 5 1 7 1 1
  values : num [1:9] 0 1 2 1 0 1 0 1 0



> test
   station_id                time bikes
1           1 2017-12-25 00:00:02     0
2           1 2017-12-25 00:01:02     0
3           1 2017-12-25 00:02:02     0
4           1 2017-12-25 00:03:02     1
5           2 2017-12-25 00:04:02     1
6           2 2017-12-25 00:05:02     2
7           2 2017-12-25 00:06:02     2
8           2 2017-12-25 00:07:02     2
9           2 2017-12-25 00:08:02     1
10          3 2017-12-25 00:09:02     0
11          3 2017-12-25 00:10:02     0
12          3 2017-12-25 00:11:02     0
13          3 2017-12-25 00:12:02     0
14          3 2017-12-25 00:13:02     0
15          4 2017-12-25 00:14:03     1
16          4 2017-12-25 00:15:02     0
17          4 2017-12-25 00:16:02     0
18          4 2017-12-25 00:17:02     0
19          4 2017-12-25 00:18:02     0
20          5 2017-12-25 00:19:02     0
21          5 2017-12-25 00:20:02     0
22          5 2017-12-25 00:21:02     0
23          5 2017-12-25 00:22:02     1
24          5 2017-12-25 00:23:02     0

我的目标是更进一步,并产生一个输出,该输出按 staiton_id 分组,并且 returns 仅当 test$bikes 具有时的时间差(以分钟为单位)重复的零。对于每个站点,这可能会发生多次(例如,test 数据中的站点 5)。最后,上述数据集将产生以下输出:

> output
  station_id diff_time      interval
1          1         2 00:00 - 00:02
2          3         4 00:09 - 00:13
3          4         3 00:15 - 00:18
4          5         2 00:19 -00:21
5          5         0 00:23 - 00:23

任何有关如何使用 dplyrrle 执行此操作的建议,我们将不胜感激!

这里是测试数据:

> dput(test)
structure(list(station_id = c(1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 
3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5), time = structure(c(1514178002.88487, 
1514178062.99145, 1514178122.88463, 1514178182.63461, 1514178242.71401, 
1514178302.20358, 1514178362.13263, 1514178422.88907, 1514178482.6502, 
1514178542.59171, 1514178602.51222, 1514178662.23203, 1514178722.04015, 
1514178782.87382, 1514178843.02124, 1514178902.71852, 1514178962.6987, 
1514179022.42077, 1514179082.19535, 1514179142.97175, 1514179202.81556, 
1514179262.85187, 1514179322.66264, 1514179382.50223), class = c("POSIXct", 
"POSIXt"), tzone = ""), bikes = c(0, 0, 0, 1, 1, 2, 2, 2, 1, 
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0)), .Names = c("station_id", 
"time", "bikes"), row.names = c(NA, 24L), class = "data.frame")

我们可以使用 data.table 中的 rleid。根据'bikes'的运行-length-id创建分组变量('grp'),然后按'station_id'和'grp'分组,指定i 其中 'bikes' 为 0,通过获取 'time' 的最后一次和第一次观察的 difftime 以及 paste format 对应的 paste 总结输出=24=] 个元素

library(data.table)
setDT(test)[, grp:= rleid(bikes)][bikes==0, 
 .(diff_time = as.numeric(round(difftime(time[.N], time[1], unit = "min"))), 
  interval = paste(format(time[1], "%M:%S"), format(time[.N], "%M:%S"), sep=" - ")),
          .(station_id, grp)]

dplyr中没有类似的函数'rleid'

所以这里创建了另一个附加功能

rle_dplyr <- function(x){
  x = rle(x)$lengths
  rep(seq_along(x),times = x)
}



> head(test1)

  station_id                time bikes
1          1 2017-12-25 10:30:02     0
2          1 2017-12-25 10:31:02     0
3          1 2017-12-25 10:32:02     0
4          1 2017-12-25 10:33:02     1
5          2 2017-12-25 10:34:02     1
6          2 2017-12-25 10:35:02     2


library(tidyverse)

test1%>%
     mutate(idrle = rle_dplyr(bikes))%>%
     filter(bikes == 0)%>%
     group_by(station_id,idrle)%>%
     summarise(diff_time = last(minute(time)) - first(minute(time)),
          interval = paste(format(first(time),"%M:%S"),format(last(time),"%M:%S"),sep = "-"))%>%
     select(-idrle) 

输出

# A tibble: 5 x 3
# Groups:   station_id [4]
  station_id diff_time    interval
       <dbl>     <int>       <chr>
1          1         2 30:02-32:02
2          3         4 39:02-43:02
3          4         3 45:02-48:02
4          5         2 49:02-51:02
5          5         0 53:02-53:02