累计客户保留

  • 本文关键字:保留 客户 r
  • 更新时间 :
  • 英文 :


我有一个客户表(cust_id(,period&订阅任务是以从一个时期到另一个时期的累积方式计算客户保留率,当客户订阅(sub(为1-活动sub或0-取消时,要注意客户订阅。

((时段1具有1、2、3个独特的客户&期间2有1,2,4个独特客户,两个客户从期间1进入期间2,因此保留率为2/3=0.667。分母是第1期间的客户数(1,2,3(。((第2期有1,2,4期,第3期有3,4,5期,但客户1已停止订阅,因此我们不一定希望看到1期,而且还有第1期的客户3,因此保留率为1/3=0.0.3333。分母是从周期2&第1期的客户3。((第3期有3、4、5,第4期有2、3、6,但客户2仍在订阅,因此累计客户保留率为1/4=0.25。分母是从周期3&期间1的客户2也在期间2中。

表格

customer_table <- structure(list(cust_id = c(1, 2, 3, 1, 4, 2, 2, 1, 5, 3, 3, 4, 
3, 2, 2, 6, 5, 4, 2, 4), period = c(1, 1, 1, 1, 2, 2, 2, 2, 
3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5), Subscription = c(1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1)), .Names = c("cust_id", "period", "Subscription"
), row.names = c(NA, -20L), class = "data.frame")
cust_id period Subscription
1        1      1            1
2        2      1            1
3        3      1            1
4        1      1            1
5        4      2            1
6        2      2            1
7        2      2            1
8        1      2            0
9        5      3            1
10       3      3            1
11       3      3            1
12       4      3            1
13       3      4            0
14       2      4            1
15       2      4            1
16       6      4            1
17       5      5            1
18       4      5            1
19       2      5            1
20       4      5            1

使用的代码缺少累积元素,也不确定取消订阅的排除是否正确应用

library(dplyr)
customer_table %>% 
group_by(period) %>% 
summarise(retention=length(intersect(cust_id,customer_table$cust_id[customer_table$period==(period+1)]))/n_distinct(cust_id[customer_table$Subscription==1])) %>% 
mutate(retention=lag(retention))

输出错误

period retention
<dbl>     <dbl>
1      1     NA   
2      2      0.5 
3      3      0.25
4      4      0.25
5      5      0.25

要求输出

period retention
<dbl>     <dbl>
1      1     NA   
2      2      0.666 
3      3      0.33
4      4      0.25
5      5      0.33

cust_id[Subscription==1]替换cust_id[customer_table$Subscription==1]使结果更接近所需的输出

# THIS IS NOT THE CORRECT SOLUTION, BUT IT'S CLOSER
customer_table %>% 
group_by(period) %>% 
summarise(retention= length(intersect(cust_id,.$cust_id[.$period== period+1]))/n_distinct(cust_id[Subscription==1])) %>% 
mutate(retention=lag(retention))
period retention
<int>     <dbl>
1      1    NA    
2      2     0.667
3      3     0.5  
4      4     0.333
5      5     0.5  

最新更新