簡體   English   中英

如何在 R 中創建一個時間間隔來計算該時間間隔中的行數

[英]How to create a time interval that count the rows in such time interval in R

我有一個數據框,用於存儲來自呼叫中心的呼叫記錄。 我的目的是統計每個時間間隔存在多少條記錄,例如,在30分鍾的時間間隔內,可能有3條通話記錄(即在該特定時間間隔內輸入了3條通話); 如果該時間間隔沒有記錄,那么我的計數器應該顯示一個零值。

這篇文章很有用,但我沒有實現,當某個時間間隔內沒有記錄時,它會顯示零值。

這是我的call_log的結構:

Classes ‘data.table’ and 'data.frame':  24416 obs. of  23 variables:
$ closecallid   : int  1145000 1144998 1144997 1144996 1144995 1144991 1144989 1144987 1144986 1144984 ...
$ lead_id       : int  1167647 1167645 1167644 1167643 1167642 1167638 1167636 1167634 1167633 1167631 ...
$ list_id       :integer64 998 998 998 998 998 998 998 998 ... 
$ campaign_id   : chr  "212120" "212120" "212120" "212120" ...
$ call_date     : POSIXct, format: "2019-08-26 20:25:30" "2019-08-26 19:32:28" "2019-08-26 19:27:03" ...
$ start_epoch   : POSIXct, format: "2019-08-26 20:25:30" "2019-08-26 19:32:28" "2019-08-26 19:27:03" ...
$ end_epoch     : POSIXct, format: "2019-08-26 20:36:25" "2019-08-26 19:44:52" "2019-08-26 19:40:23" ...
$ length_in_sec : int  655 744 800 1109 771 511 640 153 757 227 ...
$ status        : chr  "Ar" "Ar" "Ar" "Ar" ...
$ phone_code    : chr  "1" "1" "1" "1" ...
$ phone_number  : chr  "17035555" "43667342" "3135324788" "3214255222" ...
$ user          : chr  "jfino" "jfino" "jfino" "jfino" ...
$ comments      : chr  "AUTO" "AUTO" "AUTO" "AUTO" ...
$ processed     : chr  "N" "N" "N" "N" ...
$ queue_seconds : num  0 524 692 577 238 95 104 0 0 0 ...
$ user_group    : chr  "CEAS" "CEAS" "CEAS" "CEAS" ...
$ xfercallid    : int  0 0 0 0 0 0 0 0 0 0 ...
$ term_reason   : chr  "CALLER" "CALLER" "CALLER" "AGENT" ...
$ uniqueid      : chr  "1566869112.557969" "1566865941.557957" "1566865611.557952" "1566865127.557947" ...
$ agent_only    : chr  "" "" "" "" ...
$ queue_position: int  1 2 2 2 1 2 1 1 1 1 ...
$ called_count  : int  1 1 1 1 1 1 1 1 1 1 ...

而且,這是我的代碼

df <- setDT(call_log)[ , list(number_customers_arrive = sum(called_count)), by = cut(call_date, "30 min")]

提前致謝。

由於沒有可重現的示例,我嘗試在模擬數據框上解決此問題。 首先,我們創建一個帶有 ID 和時間的通話記錄:

library(lubridate)
library(dplyr)
library(magrittr)
set.seed(123)

# Generate 100 random call times during a day
calls.df <- data.frame(id=seq(1,100,1), calltime=sample(seq(as.POSIXct('2019/10/01'),
     as.POSIXct('2019/10/02'), by="min"), 100))

您的呼叫數據中可能沒有表示所有時間間隔,因此請生成所有 30 分鍾區間的序列,以防萬一:

full.df <- data.frame(bin=seq(as.POSIXct('2019/10/01'), as.POSIXct('2019/10/02'), by="30 min"))

接下來統計表示的 bin 中的調用計數:

calls.df %>% arrange(calltime) %>% mutate(diff=interval(lag(calltime),calltime)) %>% 
     mutate(mins=diff@.Data/60) %>% select(-diff) %>% 
     mutate(bin=floor_date(calltime, unit="30 minutes")) %>% 
     group_by(bin) %>% tally() -> orig.counts

現在確保未表示的 bin 為零:

right_join(orig.counts,full.df,by="bin") %>% mutate(count=ifelse(is.na(n), 0, n))

  # A tibble: 49 x 3
     bin                     n count
     <dttm>              <int> <dbl>
   1 2019-10-01 00:00:00     2     2
   2 2019-10-01 00:30:00     1     1
   3 2019-10-01 01:00:00     2     2
   4 2019-10-01 01:30:00    NA     0
   5 2019-10-01 02:00:00     2     2
   6 2019-10-01 02:30:00     4     4
   7 2019-10-01 03:00:00     1     1
   8 2019-10-01 03:30:00     1     1
   9 2019-10-01 04:00:00     2     2
  10 2019-10-01 04:30:00     1     1
  # ... with 39 more rows

希望這對你有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM