R:计数 15 分钟的时间间隔

ima*_*ima 5 aggregate r count intervals

我想计算大型数据集中每个工作日每 15 分钟开始的会话数量。

我的数据看起来像:

df <- 

Start_datetime       End_datetime       Duration    Volume
2016-04-01 06:20:55 2016-04-01 14:41:22  08:20:27   8.360
2016-04-01 08:22:27 2016-04-01 08:22:40  00:00:13   0.000
2016-04-01 08:38:53 2016-04-01 09:31:58  00:53:05   12.570
2016-04-01 09:33:57 2016-04-01 12:37:43  03:03:46   7.320
2016-04-01 10:05:03 2016-04-01 16:41:16  06:36:13   9.520
2016-04-01 12:07:57 2016-04-02 22:22:32  34:14:35   7.230
2016-04-01 16:56:55 2016-04-02 10:40:17  17:43:22   5.300
2016-04-01 17:29:18 2016-04-01 19:50:29  02:21:11   7.020
2016-04-01 17:42:39 2016-04-01 19:45:38  02:02:59   2.430
2016-04-01 17:47:57 2016-04-01 20:26:35  02:38:38   8.090
2016-04-01 22:00:15 2016-04-04 08:22:21  58:22:06   4.710
2016-04-02 01:12:38 2016-04-02 09:49:00  08:36:22   3.150
2016-04-02 01:32:00 2016-04-02 12:49:47  11:17:47   5.760
2016-04-02 07:28:48 2016-04-04 06:58:56  47:30:08   0.000
2016-04-02 07:55:18 2016-04-05 07:55:15  71:59:57   0.240
Run Code Online (Sandbox Code Playgroud)

我想计算每 15 分钟开始的所有开始会话,其中:

For business days
  Time                PTU    Count
  00:00:00 - 00:15:00  1       10     #(where count is the amount of sessions started between 00:00:00 and 00:15:00)
  00:15:00 - 00:30:00  2       6
  00:30:00 - 00:45:00  3       5
  00:45:00 - 01:00:00  3       3
Run Code Online (Sandbox Code Playgroud)

以此类推,周末的数据相同。

我试过剪切功能:

df$PTU <- table (cut(df$Start_datetime, breaks="15 minutes"))
data.frame(PTU)
Run Code Online (Sandbox Code Playgroud)

编辑:当我运行它时,我收到以下错误:

Error in cut.default(df$Start_datetime, breaks = "15 minutes") :'x' must be numeric
Run Code Online (Sandbox Code Playgroud)

还有一些带有 lubridate 的功能,但我似乎无法让它发挥作用。我的最终目标是创建一个如下所示的表,但间隔为 15 分钟。
在此处输入图片说明

Jor*_*eys 1

在日期时间上使用时必须记住两件事cut:

  1. 确保您的数据实际上是一个POSIXt类。我很确定你的不是,否则 R 不会使用cut.defaultbutcut.POSIXt作为一种方法。
  2. "15 minutes"应该"15 min"。看?cut.POSIXt

所以这有效:

Start_datetime <- as.POSIXct(
  c("2016-04-01 06:20:55",
    "2016-04-01 06:22:12",
    "2016-04-01 05:30:12")
)

table(cut(Start_datetime, breaks = "15 min"))
# 2016-04-01 05:30:00 2016-04-01 05:45:00 2016-04-01 06:00:00 2016-04-01 06:15:00 
#                   1                   0                   0                   2 
Run Code Online (Sandbox Code Playgroud)

请注意,输出为您提供了15 分钟间隔的开始作为表的名称。