library(nycflights13)
library(tidyverse)
nc <- filter(flights, !is.na(dep_delay), !is.na(arr_delay))
dim(flights)
## [1] 336776 19
arrange(flights, -dep_delay)
## # A tibble: 336,776 × 19
## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
## <int> <int> <int> <int> <int> <dbl> <int> <int>
## 1 2013 1 9 641 900 1301 1242 1530
## 2 2013 6 15 1432 1935 1137 1607 2120
## 3 2013 1 10 1121 1635 1126 1239 1810
## 4 2013 9 20 1139 1845 1014 1457 2210
## 5 2013 7 22 845 1600 1005 1044 1815
## 6 2013 4 10 1100 1900 960 1342 2211
## 7 2013 3 17 2321 810 911 135 1020
## 8 2013 6 27 959 1900 899 1236 2226
## 9 2013 7 22 2257 759 898 121 1026
## 10 2013 12 5 756 1700 896 1058 2020
## # ℹ 336,766 more rows
## # ℹ 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
## # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
## # hour <dbl>, minute <dbl>, time_hour <dttm>
nc %>% group_by(year, month, day) %>% summarise(mean = mean(dep_delay))
## `summarise()` has grouped output by 'year', 'month'. You can override using the
## `.groups` argument.
## # A tibble: 365 × 4
## # Groups: year, month [12]
## year month day mean
## <int> <int> <int> <dbl>
## 1 2013 1 1 11.4
## 2 2013 1 2 13.7
## 3 2013 1 3 10.9
## 4 2013 1 4 8.97
## 5 2013 1 5 5.73
## 6 2013 1 6 7.15
## 7 2013 1 7 5.42
## 8 2013 1 8 2.56
## 9 2013 1 9 2.30
## 10 2013 1 10 2.84
## # ℹ 355 more rows
nc %>% group_by(tailnum) %>% summarise(d = mean(arr_delay)) %>% arrange(d)
## # A tibble: 4,037 × 2
## tailnum d
## <chr> <dbl>
## 1 N560AS -53
## 2 N315AS -51
## 3 N517AS -40.5
## 4 N592AS -35.5
## 5 N7AYAA -35
## 6 N585AS -34.5
## 7 N915DN -34
## 8 N512AS -31.5
## 9 N594AS -31.3
## 10 N564AS -31
## # ℹ 4,027 more rows
nc %>% group_by(year, month, day) %>% summarise(first = min(dep_time), last = max(dep_time)) %>% arrange(last)
## `summarise()` has grouped output by 'year', 'month'. You can override using the
## `.groups` argument.
## # A tibble: 365 × 5
## # Groups: year, month [12]
## year month day first last
## <int> <int> <int> <int> <int>
## 1 2013 2 8 458 1728
## 2 2013 9 7 452 2246
## 3 2013 1 9 2 2252
## 4 2013 8 26 13 2258
## 5 2013 9 4 446 2258
## 6 2013 1 11 11 2304
## 7 2013 9 21 8 2306
## 8 2013 10 18 9 2317
## 9 2013 1 10 3 2320
## 10 2013 3 17 17 2321
## # ℹ 355 more rows
nc %>% group_by(month) %>% summarise(p = mean(dep_delay > 60))
## # A tibble: 12 × 2
## month p
## <int> <dbl>
## 1 1 0.0685
## 2 2 0.0694
## 3 3 0.0832
## 4 4 0.0909
## 5 5 0.0812
## 6 6 0.128
## 7 7 0.133
## 8 8 0.0794
## 9 9 0.0486
## 10 10 0.0469
## 11 11 0.0401
## 12 12 0.0936
flights %>% group_by(dest) %>% summarise(n = n_distinct(carrier)) %>% arrange(-n)
## # A tibble: 105 × 2
## dest n
## <chr> <int>
## 1 ATL 7
## 2 BOS 7
## 3 CLT 7
## 4 ORD 7
## 5 TPA 7
## 6 AUS 6
## 7 DCA 6
## 8 DTW 6
## 9 IAD 6
## 10 MSP 6
## # ℹ 95 more rows
d <- flights %>% group_by(dest) %>% summarise(count = n(), dist = mean(distance), delay = mean(arr_delay, na.rm = TRUE)) %>% filter(count > 20, dest != "HNL")
ggplot(d, aes(dist, delay)) + geom_point() + geom_smooth()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
d
## # A tibble: 96 × 4
## dest count dist delay
## <chr> <int> <dbl> <dbl>
## 1 ABQ 254 1826 4.38
## 2 ACK 265 199 4.85
## 3 ALB 439 143 14.4
## 4 ATL 17215 757. 11.3
## 5 AUS 2439 1514. 6.02
## 6 AVL 275 584. 8.00
## 7 BDL 443 116 7.05
## 8 BGR 375 378 8.03
## 9 BHM 297 866. 16.9
## 10 BNA 6333 758. 11.8
## # ℹ 86 more rows
Read R for Data Science to double check answers.