The Challenge

Consider the nycflights13 package. For each week of the year, find the plane(s) that made the most flights in the month, along with the tail number.

Solution

The following code returns only the top departer(s) in each week.

If we like we can retain all of the departures, but sort them within their week-groups. The key is to arrange() by weeknumber and by departures within weeknumber. See the code below.

## # A tibble: 109,324 x 3
## # Groups:   week_number [53]
##    week_number tailnum departures
##          <dbl> <chr>        <int>
##  1           1 N14542          17
##  2           1 N711MQ          17
##  3           1 N725MQ          17
##  4           1 N730MQ          17
##  5           1 N16561          16
##  6           1 N737MQ          16
##  7           1 N739MQ          16
##  8           1 N281JB          15
##  9           1 N509MQ          15
## 10           1 N723MQ          15
## # ... with 109,314 more rows

You can’t show the whole thing in a DT datatable, however. If you try then you get a message in the Console:

It seems your data is too big for client-side DataTables. 
You may consider server-side processing: 
https://rstudio.github.io/DT/server.html

You’ll also notice that in the knitted document the datatable shows incorrectly.

You could content yourself with finding top performers in each week:

## # A tibble: 348 x 3
## # Groups:   week_number [53]
##    week_number tailnum departures
##          <dbl> <chr>        <int>
##  1           1 N14542          17
##  2           1 N711MQ          17
##  3           1 N725MQ          17
##  4           1 N730MQ          17
##  5           1 N16561          16
##  6           1 N737MQ          16
##  7           1 N739MQ          16
##  8           2 N719MQ          17
##  9           2 N730MQ          17
## 10           2 N713MQ          16
## # ... with 338 more rows

Note that when there are ties in a group more than the requested 5 items are returned.

This table is small enough for DT to handle: