【问题标题】:How to create a time interval that count the rows in such time interval in R如何在 R 中创建一个时间间隔来计算该时间间隔中的行数
【发布时间】:2019-10-04 22:16:49
【问题描述】:

我有一个数据框,用于存储来自呼叫中心的呼叫记录。我的目的是统计每个时间间隔存在多少条记录,例如,在30分钟的时间间隔内,可能有3条通话记录(即在该特定时间间隔内输入了3条通话);如果该时间间隔没有记录,那么我的计数器应该显示一个零值。

这个post 很有用,但我没有实现,当某个时间间隔内没有记录时,它会显示一个零值。

这是我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")]

提前致谢。

【问题讨论】:

    标签: r datetime


    【解决方案1】:

    由于没有可重现的示例,我在模拟数据框上尝试解决方案。首先,我们创建一个带有 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
    

    希望这对你有帮助。

    【讨论】:

      猜你喜欢
      • 2016-08-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-15
      • 2014-02-18
      • 2016-04-10
      • 1970-01-01
      • 1970-01-01
      • 2022-06-01
      相关资源
      最近更新 更多