如何使用 data.table 按日期(月、年、日)和子组汇总结果
How to aggregate results by date (month, year, day) and subgroup using data.table
我正在使用 R 版本 3.1.3 尝试对事件日志数据中的事件进行计数。
我有一个时间戳事件的数据集。我已经清理了数据,并将其加载到 data.table 以便于操作。
列名是 OrderDate、EventDate、OrderID、EventTypeID、LocationID 和 EncounterID,
这些事件聚合为:EncounterID 有多个 orderID,每个 orderID 有多个 eventID
数据示例为:
library(data.table)
DT <- fread("OrderDate,EventDate,OrderID,EventTypeID,LocationID,EncounterID
1/12/2012 5:40,01/12/2012 05:40,100001,12344,1,5998887
1/12/2012 5:40,01/12/2012 05:49,100001,12345,1,5998887
1/12/2012 5:40,01/12/2012 06:40,100001,12345,1,5998887
1/12/2012 5:45,01/12/2012 05:45,100002,12344,1,5998887
1/12/2012 5:45,01/12/2012 05:49,100002,12345,1,5998887
1/12/2012 5:45,01/12/2012 06:40,100002,12345,1,5998887
1/12/2012 5:46,01/12/2012 05:46,100003,12344,2,5948887
1/12/2012 5:46,01/12/2012 05:49,100003,12345,2,5948887
1/12/2013 7:40,01/12/2013 07:40,123001,12345,2,6008887
1/12/2013 7:40,01/12/2013 07:41,123001,12346,2,6008887
1/12/2013 7:40,01/12/2013 07:50,123001,12345,2,6008887
1/12/2013 7:40,01/12/2013 07:55,123001,12345,2,6008887")
DT$OrderDate <- as.POSIXct(DT$OrderDate, format="%d/%m/%Y %H:%M")
DT$EventDate <- as.POSIXct(DT$EventDate, format="%d/%m/%Y %H:%M")
我的最终目标是使用 ggplot2 直观地探索这些数据,按月查看各种组合的计数...但是我在使用 data.table 的[=15= 汇总数据时遇到了问题]
我的具体问题(一个示例)如何生成以下内容的 table:
月-年,位置 ID,Count_of_Orders
如果我执行以下操作:
DT[,.N,by=.(month(OrderDate),year(OrderDate))]
我得到了所有 eventID 的计数,但我需要每个 locationID 每月的 OrderID 计数。
month year N
1: 12 2012 8
2: 12 2013 4
但是 - 我正在寻找的是按 LocationID 按月年 N 的结果:
Month-Year,LocationID,Count_of_orders
01-12,1,2
01-12,2,1
01-13,1,0
01-13,2,1
注意:请注意,对于一个月内没有订单的任何位置,它们应该以零计数列出。因此,需要通过生成唯一位置 ID 列表来确定位置。
有人可以提供解决方案吗?
谢谢
我假设你的 date/times 是 POSIXct
格式(因为你调用 month
/year
)。那么,
d[, month.year := format(OrderDate, '%m-%y')]
setkey(d, month.year, LocationID, OrderID)
unique(d)[CJ(unique(month.year), unique(LocationID)), .N, by = .EACHI]
# month.year LocationID N
#1: 01-12 1 2
#2: 01-12 2 1
#3: 01-13 1 0
#4: 01-13 2 1
我使用的事实是 unique
默认情况下会通过键选择唯一条目,并且还会保留键,所以我可以轻松地进行下一次连接。
我正在使用 R 版本 3.1.3 尝试对事件日志数据中的事件进行计数。
我有一个时间戳事件的数据集。我已经清理了数据,并将其加载到 data.table 以便于操作。
列名是 OrderDate、EventDate、OrderID、EventTypeID、LocationID 和 EncounterID,
这些事件聚合为:EncounterID 有多个 orderID,每个 orderID 有多个 eventID
数据示例为:
library(data.table)
DT <- fread("OrderDate,EventDate,OrderID,EventTypeID,LocationID,EncounterID
1/12/2012 5:40,01/12/2012 05:40,100001,12344,1,5998887
1/12/2012 5:40,01/12/2012 05:49,100001,12345,1,5998887
1/12/2012 5:40,01/12/2012 06:40,100001,12345,1,5998887
1/12/2012 5:45,01/12/2012 05:45,100002,12344,1,5998887
1/12/2012 5:45,01/12/2012 05:49,100002,12345,1,5998887
1/12/2012 5:45,01/12/2012 06:40,100002,12345,1,5998887
1/12/2012 5:46,01/12/2012 05:46,100003,12344,2,5948887
1/12/2012 5:46,01/12/2012 05:49,100003,12345,2,5948887
1/12/2013 7:40,01/12/2013 07:40,123001,12345,2,6008887
1/12/2013 7:40,01/12/2013 07:41,123001,12346,2,6008887
1/12/2013 7:40,01/12/2013 07:50,123001,12345,2,6008887
1/12/2013 7:40,01/12/2013 07:55,123001,12345,2,6008887")
DT$OrderDate <- as.POSIXct(DT$OrderDate, format="%d/%m/%Y %H:%M")
DT$EventDate <- as.POSIXct(DT$EventDate, format="%d/%m/%Y %H:%M")
我的最终目标是使用 ggplot2 直观地探索这些数据,按月查看各种组合的计数...但是我在使用 data.table 的[=15= 汇总数据时遇到了问题]
我的具体问题(一个示例)如何生成以下内容的 table: 月-年,位置 ID,Count_of_Orders
如果我执行以下操作:
DT[,.N,by=.(month(OrderDate),year(OrderDate))]
我得到了所有 eventID 的计数,但我需要每个 locationID 每月的 OrderID 计数。
month year N
1: 12 2012 8
2: 12 2013 4
但是 - 我正在寻找的是按 LocationID 按月年 N 的结果:
Month-Year,LocationID,Count_of_orders
01-12,1,2
01-12,2,1
01-13,1,0
01-13,2,1
注意:请注意,对于一个月内没有订单的任何位置,它们应该以零计数列出。因此,需要通过生成唯一位置 ID 列表来确定位置。
有人可以提供解决方案吗?
谢谢
我假设你的 date/times 是 POSIXct
格式(因为你调用 month
/year
)。那么,
d[, month.year := format(OrderDate, '%m-%y')]
setkey(d, month.year, LocationID, OrderID)
unique(d)[CJ(unique(month.year), unique(LocationID)), .N, by = .EACHI]
# month.year LocationID N
#1: 01-12 1 2
#2: 01-12 2 1
#3: 01-13 1 0
#4: 01-13 2 1
我使用的事实是 unique
默认情况下会通过键选择唯一条目,并且还会保留键,所以我可以轻松地进行下一次连接。