Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 204771

Finding the longest consecutive streak in a tibble column

$
0
0

I have the following tibble:

my_tbl <- tribble(
  ~year, ~event_id, ~winner_id,
  2011,      "A",     4322,
  2011,      "A",     9604,
  2012,      "A",     4322,
  2012,      "A",     9604,
  2012,      "A",     1663,
  2013,      "A",     4322,
  2014,      "A",     5478,
  2015,      "A",     4322,
  2015,      "A",     1663,
  2011,      "B",     4322,
  2012,      "B",     7893,
  2012,      "B",     1188,
  2013,      "B",     7893,
  2014,      "B",     2365,
  2015,      "B",     3407,
  2011,      "C",     5556,
  2012,      "C",     5556,
  2013,      "C",     1238,
  2014,      "C",     2391,
  2015,      "C",     2391,
  2011,      "D",     4219,
  2012,      "D",     7623,
  2013,      "D",     8003,
  2014,      "D",     2851,
  2015,      "D",     0418
)

I would like to find out the most wins in a row by event id. The result I'm looking for would look like this:

results_summary_tbl <- tribble(
  ~event_id, ~most_wins_in_a_row, ~number_of_winners, ~winners,                              ~years,
   "A",       3,                  1,                   "4322",                               "4322 = (2011, 2012, 2013)",
   "C",       2,                  2,                   "5556 , 2391",                        "5556 = (2011, 2012), 2391 = (2014, 2015)",
   "B",       2,                  1,                   "7893",                               "7893 = (2012, 2013)",
   "D",       1,                  5,                   "4219 , 7623 , 8003 , 2851 , 0418",   "4219 = (2011), 7623 = (2012), 8003 = (2013), 2851 = (2014), 0418 = (2015)"
)

thank-you


Viewing all articles
Browse latest Browse all 204771

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>