【发布时间】:2015-11-16 19:14:42
【问题描述】:
我正在尝试构建一个流失模型,其中包括每个客户的最大连续 UX 失败次数并遇到问题。这是我的简化数据和所需的输出:
library(dplyr)
df <- data.frame(customerId = c(1,2,2,3,3,3), date = c('2015-01-01','2015-02-01','2015-02-02', '2015-03-01','2015-03-02','2015-03-03'),isFailure = c(0,0,1,0,1,1))
> df
customerId date isFailure
1 1 2015-01-01 0
2 2 2015-02-01 0
3 2 2015-02-02 1
4 3 2015-03-01 0
5 3 2015-03-02 1
6 3 2015-03-03 1
想要的结果:
> desired.df
customerId maxConsecutiveFailures
1 1 0
2 2 1
3 3 2
我一直在胡思乱想,搜索其他 rle 问题并没有帮助我——这就是我“期待”的解决方案:
df %>%
group_by(customerId) %>%
summarise(maxConsecutiveFailures =
max(rle(isFailure[isFailure == 1])$lengths))
【问题讨论】:
-
基本 R 选项是
sapply(split(df$isFailure, df$customerId), function(x) {tmp <- with(rle(x==1), lengths[values]); if(length(tmp)==0) 0 else tmp}) -
data.table的另一个选项是setDT(df)[, {tmp <- rleid(isFailure)*isFailure; tmp2 <- table(tmp[.N==1|tmp!=0]); max((names(tmp2)!=0)*tmp2)}, customerId][]