Summarize consecutive failures with dplyr and rle

后端 未结 2 1212
[愿得一人]
[愿得一人] 2021-01-20 03:56

I\'m trying to build a churn model that includes the maximum consecutive number of UX failures for each customer and having trouble. Here\'s my simplified data and desired o

2条回答
  •  清歌不尽
    2021-01-20 04:15

    We group by the 'customerId' and use do to perform the rle on 'isFailure' column. Extract the lengths that are 'TRUE' for values (lengths[values]), and create the 'Max' column with an if/else condition to return 0 for those that didn't have any 1 value.

     df %>%
        group_by(customerId) %>%
        do({tmp <- with(rle(.$isFailure==1), lengths[values])
         data.frame(customerId= .$customerId, Max=if(length(tmp)==0) 0 
                        else max(tmp)) }) %>% 
         slice(1L)
    #   customerId Max
    #1          1   0
    #2          2   1
    #3          3   2
    

提交回复
热议问题