按用户定义的时间间隔汇总数据

时间:2017-09-15 19:40:06

标签: r group-by data.table aggregate plyr

我有以下数据框:

df<-data.frame(timecol=as.POSIXct(c("2016-05-31 22:12:27 PDT","2016-05-31 22:25:03 PDT","2016-05-31 23:08:43 PDT","2016-05-31 23:24:10 PDT","2016-06-01 02:00:56 PDT","2016-06-01 03:00:56 PDT","2016-06-01 05:00:56 PDT","2016-06-01 22:12:27 PDT","2016-06-01 22:25:03 PDT","2016-06-01 23:08:43 PDT","2016-06-01 23:24:10 PDT","2016-06-02 02:00:56 PDT","2016-06-02 03:00:56 PDT","2016-06-02 05:00:56 PDT")),value=sample(1:100,14))

> df
               timecol value
1  2016-05-31 22:12:27   100
2  2016-05-31 22:25:03    86
3  2016-05-31 23:08:43    39
4  2016-05-31 23:24:10    91
5  2016-06-01 02:00:56    32
6  2016-06-01 03:00:56    93
7  2016-06-01 05:00:56    53
8  2016-06-01 22:12:27    54
9  2016-06-01 22:25:03    76
10 2016-06-01 23:08:43    19
11 2016-06-01 23:24:10    56
12 2016-06-02 02:00:56    20
13 2016-06-02 03:00:56     3
14 2016-06-02 05:00:56    66

我需要根据预定义的时间间隔聚合值列 - 从当天晚上19点到第二天早上7点。我在想这样的事情:

tm <- seq(as.POSIXct("2016-05-31 19:00:00 PDT"),as.POSIXct("2016-06-02 07:00:00 PDT"), by = "12 hours")
aggregate(df$value, list(day = cut(tm, "days")), sum)

但我无法弄清楚出了什么问题。

0 个答案:

没有答案