R语言 提迪尔/杜尔:迭代计数,直到找到特定值,“在输入值X之前添加了多少条记录?“

xjreopfe  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(91)

我有一个呼叫日志数据集,我需要做一个迭代计数,直到找到一个特定的值。
这个想法是回答这个问题“在看到这个特定值之前输入了多少条记录?在我的真实的数据中,这个问题的措辞是“在每个人接电话之前,我们给他们打了多少次电话?”“
我的数据将有3列:
1.电话号码
1.召集日期
1.有人回答吗?(此列是文本列,答案可以是YES、NO、留言机、语音邮件等)
下面是6行示例数据:

data <- data.frame(PhoneNum = c("1112223333","1112223333","1112223333","1112223334","1112223334","1112223334"),
                   Answered = c("Voice Mail", "Answering Machine", "Member Answered", "Voice Mail", "Answering Machine", "Member Answered"),
                   Date = Sys.Date() - 1:6)

我试图做的是得到一个计数有多少次每个电话号码被称为之前,一个成员回答他们的电话。希望输出看起来像这样:

data <- data.frame(PhoneNum = c("1112223333","1112223333","1112223333","1112223334","1112223334","1112223334"),
                   Answered = c("Voice Mail", "Answering Machine", "Member Answered", "Voice Mail", "Answering Machine", "Member Answered"),
                   Date = Sys.Date() - 1:6,
                   CallAttempts = c("1", "2", "3", "1", "2", "3")) %>% filter(str_detect(Answered, "Answered"))

但是我不想失去所有的其他行,如果我能避免它。
最好的情况是做我在这里所做的样本,并创建一个新的DF只有'回答'调用?
最后,我想确定1。总共进行了多少次呼叫尝试,2。有多少电话是以“印象”结束的(无论如何,一个成员会知道我们的公司打过电话,他们需要给我们回电话。我们的一些“已回答”值将是“坏号码”或者不能为成员留言),以及3.每个成员在接听电话之前尝试了多少次(需要获得每次接听的平均尝试次数)

f87krz0w

f87krz0w1#

已应答呼叫的df(以及该电话号码的呼叫编号):(我假设Member Answer或Member Machine都应该被视为回答,基于片段“Answer”,但可以自由定义。

data |>
  arrange(Date) |>
  mutate(call_num = row_number(), .by = PhoneNum) |>
  filter(str_detect(Answered, "Answer"))

或者所有这些统计数据:

data |>
  arrange(Date) |>
  mutate(call_num = row_number(), 
         answered = stringr::str_detect(Answered, "Answer"),
         num_calls = n(),
         impressions = sum(answered),
         first_answer = min(call_num[answered]),
         .by = PhoneNum)

    PhoneNum          Answered       Date call_num answered num_calls impressions first_answer
1 1112223334   Member Answered 2023-10-06        1     TRUE         3           2            1
2 1112223334 Answering Machine 2023-10-07        2     TRUE         3           2            1
3 1112223334        Voice Mail 2023-10-08        3    FALSE         3           2            1
4 1112223333   Member Answered 2023-10-09        1     TRUE         3           2            1
5 1112223333 Answering Machine 2023-10-10        2     TRUE         3           2            1
6 1112223333        Voice Mail 2023-10-11        3    FALSE         3           2            1

相关问题