The loan goes to @Henk for an updated solution data.table:
setDT(nycflights13::flights)[month %between% c(6,9), .N, keyby = .(carrier, month)][, .SD[which.max(N)], month]
month carrier n
1: 6 UA 4975
2: 7 UA 5066
3: 8 UA 5124
4: 9 EV 4725
The original solution is in the response history.
Microobject: (for those who care)
library(microbenchmark)
microbenchmark(henk=setDT(nycflights13::flights)[month %between% c(6,9), .N, keyby = .(carrier, month)][, .SD[which.max(N)], month],
akrun=nycflights13::flights %>%
group_by(carrier,month)%>%
filter(month %in% 6:9) %>%
summarise(n = n()) %>%
group_by(month) %>%
slice(which.max(n)))
Unit: milliseconds
expr min lq mean median uq max neval
henk 5.612305 6.41659 7.416813 6.953205 7.515347 49.38172 100
akrun 45.529320 47.51715 51.943065 48.882663 49.834458 221.39357 100
source
share