计算过去30天内事件的发生次数

时间:2017-11-22 00:20:46

标签: r count find-occurrences

我想计算过去30天内联系此人的次数。以下是数据:

> df1
   ID       Date     Time Call_Status           Date_Time     x
7   1 2017-08-02  1:00:01     Contact 2017-08-02 01:00:01  TRUE
6   1 2017-09-01  1:00:00  No Contact 2017-09-01 01:00:00 FALSE
3   1 2017-09-02  2:00:00     Contact 2017-09-02 02:00:00  TRUE
2   1 2017-10-01  9:00:00     Contact 2017-10-01 09:00:00  TRUE
1   1 2017-10-01 13:00:00     Contact 2017-10-01 13:00:00  TRUE
5   2 2017-08-02  5:00:00     Contact 2017-08-02 05:00:00  TRUE
4   2 2017-09-01  3:00:00  No Contact 2017-09-01 03:00:00 FALSE
8   2 2017-11-01  2:00:00     Contact 2017-11-01 02:00:00  TRUE
10  3 2017-03-06 12:31:03  No Contact 2017-03-06 12:31:03 FALSE
11  3 2017-04-06 19:55:02     Contact 2017-04-06 19:55:02  TRUE
9   3 2017-05-02 18:00:05     Contact 2017-05-02 18:00:05  TRUE

我关心时间(小时,分钟,秒),也就是说,如果客户在一小时前联系过(即使这发生在同一天),我想将其视为一种情况。理想情况下,我想获得下表

 id     x          d        t                  dt dminus30 
5   1  TRUE 2017-10-01 13:00:00 2017-10-01 13:00:00        2        
4   1  TRUE 2017-10-01  9:00:00 2017-10-01 09:00:00        1        
3   1  TRUE 2017-09-02  2:00:00 2017-09-02 02:00:00        0        
2   1 FALSE 2017-09-01  1:00:00 2017-09-01 01:00:00        1        
1   1  TRUE 2017-08-02  1:00:01 2017-08-02 01:00:01        0        
8   2  TRUE 2017-11-01  2:00:00 2017-11-01 02:00:00        0        
7   2 FALSE 2017-09-01  3:00:00 2017-09-01 03:00:00        1        
6   2  TRUE 2017-08-02  5:00:00 2017-08-02 05:00:00        0        
11  3  TRUE 2017-05-02 18:00:05 2017-05-02 18:00:05        1        
10  3  TRUE 2017-04-06 19:55:02 2017-04-06 19:55:02        0        
9   3 FALSE 2017-03-06 12:31:03 2017-03-06 12:31:03        0  

我写的代码不能正常工作,因为它不计算小时,分钟和秒。谁能帮帮我吗?谢谢。

orderDT = with(df1,data.table(id = ID, x = x, d = Date, t = Time, dt = Date_Time))

vec = list(minus30 = 30L, minus60 = 60L)
vec

# If we don't count any calls on the same day

orderDT[,c("dminus30", "dminus60"):=.(
  orderDT[x][orderDT[,.(id,d_minus30 = d-vec$minus30, d_yest = d - 1L)], on=.
             (id, d >= d_minus30, d <= d_yest), .N, by = .EACHI] $N

  ,

  orderDT[x][orderDT[,.(id,d_minus60 = d-vec$minus60, d_yest = d - 1L)], on=.
                        (id, d >= d_minus60, d <= d_yest), .N, by = .EACHI] $N
)]


new <- as.data.frame(orderDT)

sorted_new <-new[with(new,order(new$id, desc(new$dt))),]

0 个答案:

没有答案