Summarizing logical vectors (4)

flights |>
  summarize(
    behind = mean(arr_delay[arr_delay > 0], na.rm = TRUE),
    ahead = mean(arr_delay[arr_delay < 0], na.rm = TRUE),
    n = n(),
    .by = c(year, month, day)
  )
## # A tibble: 365 × 6
##     year month   day behind ahead     n
##    <int> <int> <int>  <dbl> <dbl> <int>
##  1  2013     1     1   32.5 -12.5   842
##  2  2013     1     2   32.0 -14.3   943
##  3  2013     1     3   27.7 -18.2   914
##  4  2013     1     4   28.3 -17.0   915
##  5  2013     1     5   22.6 -14.0   720
##  6  2013     1     6   24.4 -13.6   832
##  7  2013     1     7   27.8 -17.0   933
##  8  2013     1     8   20.8 -14.3   899
##  9  2013     1     9   25.6 -13.0   902
## 10  2013     1    10   27.3 -16.4   932
## # ℹ 355 more rows