How to find the longest duplicate sequence in a tibble column?

前端 未结 2 2002

I updated my question because I need one more column to my output tible.

I have the following tibble:

library(tibble)

my_tbl <- tribble(
  ~year, ~ev         


        
2条回答
  •  一生所求
    2021-01-22 15:08

    One dplyr option could be:

    my_tbl %>%
     add_count(event_id, rleid = cumsum(winner_id != lag(winner_id, default = first(winner_id)))) %>%
     group_by(event_id) %>%
     summarise(most_wins_in_a_row = max(n),
               number_of_winners = n_distinct(winner_id[n == max(n)]),
               winners = paste0(unique(winner_id[n == max(n)]), collapse = ","))
    
      event_id most_wins_in_a_row number_of_winners winners                
                                                       
    1 A                         3                 1 4322                   
    2 B                         2                 1 7893                   
    3 C                         2                 2 5556,2391              
    4 D                         1                 5 4219,7623,8003,2851,418
    

提交回复
热议问题