首页 文章

如何使用data.table按日期(月,年,日)和子组聚合结果

提问于
浏览
3

使用R版本3.1.3我试图计算事件日志数据中的事件 .

我有一个timstamped事件的数据集 . 我已经清理了数据,并将其加载到data.table中以便于操作 .

Colnames是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的聚合数据时遇到了问题 .

我的具体问题(一个示例)如何生成以下表格:Month-Year,LocationID,Count_of_Orders

如果我执行以下操作:

DT[,.N,by=.(month(OrderDate),year(OrderDate))]

我得到了所有eventID的计数,但我需要每个locationID每月的OrderID .

month year N
1:    12 2012 8
2:    12 2013 4

但是 - 我正在寻找的是按位置ID划分的N by Month-year的结果:

Month-Year,LocationID,Count_of_orders
01-12,1,2
01-12,2,1
01-13,1,0
01-13,2,1

注意:请注意,对于任何一个月内没有订单的地点,它们应列在计数零处 . 因此,需要通过生成唯一locationID列表来确定位置 .

有人可以提供解决方案吗?

谢谢

1 回答

  • 2

    我假设您的日期/时间是 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 默认情况下会通过键选择唯一的条目,并且还会保留密钥,因此我可以轻松地进行下一次连接 .

相关问题