Skip to content

Instantly share code, notes, and snippets.

@the-yuyut
Created January 10, 2026 05:38
Show Gist options
  • Select an option

  • Save the-yuyut/00f62a6e80e85b085a1c8b02d36fcda0 to your computer and use it in GitHub Desktop.

Select an option

Save the-yuyut/00f62a6e80e85b085a1c8b02d36fcda0 to your computer and use it in GitHub Desktop.
Summarise Tables R
# Code 2 — Adjust for “bad airports”: rank carriers by delay relative to each destination’s average
library(dplyr)
dest_avg <- flights %>%
group_by(dest) %>%
summarise(
dest_avg_delay = mean(arr_delay, na.rm = TRUE),
.groups = "drop"
)
flights %>%
left_join(dest_avg, by = "dest") %>%
mutate(rel_delay = arr_delay - dest_avg_delay) %>%
group_by(carrier) %>%
summarise(
"n(delay)" = n(),
avg_rel_delay = mean(rel_delay, na.rm = TRUE),
.groups = "drop"
) %>%
mutate(rank = min_rank(desc(avg_rel_delay))) %>%
arrange(rank)
flights %>%
select(tailnum, year, month, day, dep_delay,arr_delay) %>%
mutate(bi_delays = arr_delay + dep_delay) %>%
filter(
dep_delay > 60 |
arr_delay > 60 |
bi_delays > 60
) %>%
arrange(arr_delay, dep_delay, bi_delays)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment