计算按年划分的保留率
Calculate retention rate split by year
按年度计算保留率/流失率
亲爱的社区,我正在从事一个数据挖掘项目,我想将先前的想法从 excel 转换为 R。
我有一个包含合同数据的客户数据库,我想计算保留率。
我在玩这些library(lubridate)
; library(reshape2)
; library(plyr)
但我不知道它在 R 中是如何工作的。
我有这样的数据:
ID Customer START END
1 Tesco 01-01-2000 31-12-2000
2 Apple 05-11-2001 06-02-2002
3 H&M 01-02-2002 08-05-2002
4 Tesco 01-01-2001 31-12-2001
5 Apple 01-01-2003 31-12-2004
我现在正在考虑将数据拆分为年份 (df2000, df2001) 然后再查找是否客户名称存在于主 table(如果是 return 1) .
结果可能如下所示:
Customer 2000 2001 2002 2003 Retention Rate
Tesco 1 1 0 0 0.5
Apple 0 1 0 1
H&M 0 0 1 0
使用 dplyr
,您可以尝试从每个 START
日期获取 year
值,每个 Customer
和 [=] 的条目数 count
14=],计算保留率和spread
数据到宽格式。
library(dplyr)
df %>%
mutate(year = format(as.Date(START, format = "%d-%m-%Y"), "%Y")) %>%
dplyr::count(Customer, year) %>%
group_by(Customer) %>%
mutate(ret = n()/n_distinct(.$year)) %>%
tidyr::spread(year, n, fill = 0)
# Customer ret `2000` `2001` `2002` `2003`
# <fct> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 Apple 0.5 0 1 0 1
#2 H&M 0.25 0 0 1 0
#3 Tesco 0.5 1 1 0 0
编辑
要考虑会计年度而不是 10 月至 9 月的数据,我们可以这样做
library(lubridate)
df %>%
mutate(START = dmy(START),
START = if_else(month(START) >= 10, START + years(1), START),
year = year(START)) %>%
dplyr::count(Customer, year) %>%
group_by(Customer) %>%
mutate(ret = n()/n_distinct(.$year)) %>%
tidyr::spread(year, n, fill = 0)
数据
df <- structure(list(ID = 1:5, Customer = structure(c(3L, 1L, 2L, 3L,
1L), .Label = c("Apple", "H&M", "Tesco"), class = "factor"),
START = structure(c(1L, 5L, 4L, 2L, 3L), .Label = c("01-01-2000",
"01-01-2001", "01-01-2003", "01-02-2002", "05-11-2001"), class = "factor"),
END = structure(c(3L, 1L, 2L, 4L, 5L), .Label = c("06-02-2002",
"08-05-2002", "31-12-2000", "31-12-2001", "31-12-2004"), class = "factor")),
class = "data.frame", row.names = c(NA, -5L))
按年度计算保留率/流失率
亲爱的社区,我正在从事一个数据挖掘项目,我想将先前的想法从 excel 转换为 R。
我有一个包含合同数据的客户数据库,我想计算保留率。
我在玩这些library(lubridate)
; library(reshape2)
; library(plyr)
但我不知道它在 R 中是如何工作的。
我有这样的数据:
ID Customer START END
1 Tesco 01-01-2000 31-12-2000
2 Apple 05-11-2001 06-02-2002
3 H&M 01-02-2002 08-05-2002
4 Tesco 01-01-2001 31-12-2001
5 Apple 01-01-2003 31-12-2004
我现在正在考虑将数据拆分为年份 (df2000, df2001) 然后再查找是否客户名称存在于主 table(如果是 return 1) .
结果可能如下所示:
Customer 2000 2001 2002 2003 Retention Rate
Tesco 1 1 0 0 0.5
Apple 0 1 0 1
H&M 0 0 1 0
使用 dplyr
,您可以尝试从每个 START
日期获取 year
值,每个 Customer
和 [=] 的条目数 count
14=],计算保留率和spread
数据到宽格式。
library(dplyr)
df %>%
mutate(year = format(as.Date(START, format = "%d-%m-%Y"), "%Y")) %>%
dplyr::count(Customer, year) %>%
group_by(Customer) %>%
mutate(ret = n()/n_distinct(.$year)) %>%
tidyr::spread(year, n, fill = 0)
# Customer ret `2000` `2001` `2002` `2003`
# <fct> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 Apple 0.5 0 1 0 1
#2 H&M 0.25 0 0 1 0
#3 Tesco 0.5 1 1 0 0
编辑
要考虑会计年度而不是 10 月至 9 月的数据,我们可以这样做
library(lubridate)
df %>%
mutate(START = dmy(START),
START = if_else(month(START) >= 10, START + years(1), START),
year = year(START)) %>%
dplyr::count(Customer, year) %>%
group_by(Customer) %>%
mutate(ret = n()/n_distinct(.$year)) %>%
tidyr::spread(year, n, fill = 0)
数据
df <- structure(list(ID = 1:5, Customer = structure(c(3L, 1L, 2L, 3L,
1L), .Label = c("Apple", "H&M", "Tesco"), class = "factor"),
START = structure(c(1L, 5L, 4L, 2L, 3L), .Label = c("01-01-2000",
"01-01-2001", "01-01-2003", "01-02-2002", "05-11-2001"), class = "factor"),
END = structure(c(3L, 1L, 2L, 4L, 5L), .Label = c("06-02-2002",
"08-05-2002", "31-12-2000", "31-12-2001", "31-12-2004"), class = "factor")),
class = "data.frame", row.names = c(NA, -5L))