【问题标题】:How to calculate avg response time & total response time based on group_by cols and timestamps using R?如何使用 R 根据 group_by cols 和时间戳计算平均响应时间和总响应时间?
【发布时间】:2021-09-15 19:55:28
【问题描述】:

我有一个看起来像这样的表(1 个示例 - 总共 200 万行):

tweet_id |  id         |  group   |                created_at         | tweet                                                 |  response_tweet_id
1           sprintcare    Support      Tue Oct 31 22:10:47 +0000 2017   @115712 I understand. I would like to assist you.        2
2           115712        Customer     Tue Oct 31 22:11:45 +0000 2017   @sprintcare and how do you propose we do that?           NA
3           115712        Customer     Tue Oct 31 22:08:27 +0000 2017   @sprintcare I have sent several private messages.        1
4           sprintcare    Support      Tue Oct 31 21:54:49 +0000 2017   @115712 Please send us a Private Message.                3
5           115712        Customer     Tue Oct 31 21:49:35 +0000 2017   @sprintcare I did.                                       4
6           sprintcare    Support      Tue Oct 31 21:46:24 +0000 2017   @115712 Can you please send us a private message.        5,7
8           115712        Customer     Tue Oct 31 21:45:10 +0000 2017   @sprintcare is the worst customer service                9,6,10
11          apple         Support      Tue Oct 31 22:10:35 +0000 2017  @115713 This is saddening to hear.                        NA
12          115713        Customer     Tue Oct 31 22:04:47 +0000 2017   @apple My phone is not working. Help!               11

引用第一行时,我们可以看到响应 tweet_id 1(标记为 2)时第一个支持 tweet 响应的时间。

理想情况下 - 我想计算每条响应推文需要多长时间 - 从支持到客户订购。

我想计算两个值:

  1. 向支持人员发出第一条推文之间的响应时间(tweet_id、response_tweet_id 和 created_at)。对于第一行 - 时差是: 2017 年 10 月 31 日星期二 22:10:47 +0000 - 2017 年 10 月 31 日星期二 22:08:27 +0000 = 00:02:20。

  2. 从第一条推文从支持到给每个客户的最后一条推文之间的总响应时间。在下面的示例中 - 它本质上是相对于支持时间发出的第一条推文,以及在下一个唯一 ID 发挥作用之前发出的最后一条推文。

根据每个组(100 多家独特的公司),所需的输出如下所示:

id         | group  | Avg_response_time_per_tweet (in minutes) | Total_avg_response_time (in minutes)
sprintcare   Support  ####                                       ####
apple        Support  ####                                       #### 

【问题讨论】:

  • 当你说平均响应时间时,你想怎么计算。是否基于 sprintcare 的 created_at 相对于 115712 值的差异,然后取每次 sprintcare 出现的平均值
  • 好的,平均响应时间是多少?是从第一条推文到第二条的时间吗?还是群组中最后一条推文的第一条推文?
  • 预期的并不完全清楚。也许df1 %>% mutate(created_at = as.POSIXct(created_at, format = '%a %b %d %H:%M:%S +0000 %Y')) %>% group_by(group2 = cumsum(group == "Support")) %>% summarise(id = first(id), group = first(group), avg = mean(difftime(first(created_at), created_at[-1], units = "sec")))
  • 抱歉 - 我根据您的两个问题更新了所需的输出。

标签: r datetime dplyr tidytext


【解决方案1】:
library(lubridate)
library(dplyr)
library(tidyr)
df %>% 
    separate(created_at, c("Day_name", "Month", "Day", "Hour", "Minute", "Second", "X", "Year")) %>% 
    type.convert(as.is = TRUE) %>% 
    mutate(Month = match(Month, month.abb)) %>% 
    mutate(created_at = make_datetime(Year, Month, Day, Hour, Minute, Second), .keep="unused") %>% 
    group_by(id, group) %>% 
    summarise(Avg_response_time = mean(difftime(max(created_at), min(created_at))))
  id         group    Avg_response_time
  <chr>      <chr>    <drtn>           
1 115712     Customer 26.58333 mins    
2 sprintcare Support  24.38333 mins  

【讨论】:

  • 感谢您的回复 - 这很有帮助 - 我认为我的问题之前有点模糊,所以我添加了更多数据点,以便更清楚地计算每个公司的时间。
【解决方案2】:

这不是一个真正高效的方法,但它应该会产生预期的结果。

library(dplyr)
library(stringr)

dat %>% 
  mutate(id_at = str_extract(tweet, "\\d+"),
         global_id = ifelse(is.na(id_at), id, id_at)) %>% 
  group_by(global_id) %>% 
  group_modify(~ .x %>%
                 rowwise %>% 
                 mutate(response_time = if(group == "Support") {
                   filter(.x, created_at < .env$created_at, group == "Customer") %>% 
                     slice_max(created_at, n = 1) %>% pull(created_at)
                 } else NA)
  ) %>% 
  summarise(id = first(id), Total_avg_response_time = difftime(max(created_at), min(created_at)),
            Avg_response_time_per_tweet = mean(difftime(created_at, response_time), na.rm = TRUE))

#> # A tibble: 1 x 4
#>   global_id id         Total_avg_response_time Avg_response_time_per_tweet
#>   <chr>     <chr>      <drtn>                  <drtn>                     
#> 1 115712    sprintcare 26.58333 mins           2.933333 mins

# data used
dat <- read.table(text = "tweet_id   id             group                  created_at            tweet
                  1           sprintcare    Support      'Tue Oct 31 22:10:47 +0000 2017'   '@115712 I understand. I would like to assist you.'
                  2           115712        Customer     'Tue Oct 31 22:11:45 +0000 2017'   '@sprintcare and how do you propose we do that'
                  3           115712        Customer     'Tue Oct 31 22:08:27 +0000 2017'   '@sprintcare I have sent several private messages and no one is responding as usual'
                  4           sprintcare    Support      'Tue Oct 31 21:54:49 +0000 2017'   '@115712 Please send us a Private Message so that we can further assist you.'
                  5           115712        Customer     'Tue Oct 31 21:49:35 +0000 2017'   '@sprintcare I did.'
                  6           sprintcare    Support      'Tue Oct 31 21:46:24 +0000 2017'   '@115712 Can you please send us a private message, so that I can gain further details about your account?'
                  7           115712        Customer     'Tue Oct 31 21:45:10 +0000 2017'   '@sprintcare is the worst customer service'",
                  header = TRUE)

dat <- dat %>% 
  mutate(created_at = strptime(created_at, "%a %b %d %H:%M:%S %z %Y"))

reprex package (v0.3.0) 于 2021 年 9 月 15 日创建

【讨论】:

  • 谢谢!我为数据添加了更多上下文,以便更轻松地计算 @TimTeaFan。
  • @Dinho:我的方法对于 200 万行并不是很有效,但它应该会在您之后产生结果——即使是更新数据。尝试在您的数据子集上进行验证。
  • @TimeTeaFan - 我确实在我的数据集上运行了你的代码 - sample_n(15000) - 我收到了这个错误。错误:mutate()response_time 有问题。我response_time = if (...) NULL。 i response_time 的大小必须为 1,而不是 0。我的意思是:response_time = list(if (...) NULL)? i 错误发生在第 1 行。
  • @Dinho:我上面的例子对你有用吗?如果不是,则可能是 R 版本/包版本问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-31
  • 1970-01-01
  • 2023-04-01
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多