Lecture 9 Exercises
Lecture 9 Exercises
Lecture 9 Exercises
AUTHOR
Hamzah Syed
Exercise 1
library(tidyverse)
library(nycflights13)
flights |>
filter(arr_delay >= 120, carrier %in% c("UA", "AA", "DL"))
# A tibble: 3,280 × 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 1 957 733 144 1056 853
2 2013 1 1 1114 900 134 1447 1222
3 2013 1 1 1856 1645 131 2212 2005
4 2013 1 1 2205 1720 285 46 2040
5 2013 1 2 833 558 155 1018 727
6 2013 1 2 1412 838 334 1710 1147
7 2013 1 2 1451 1232 139 1749 1533
8 2013 1 2 1607 1030 337 2003 1355
9 2013 1 2 1751 1450 181 2041 1755
10 2013 1 2 2131 1512 379 2340 1741
# ℹ 3,270 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>
Exercise 2
https://01752a68c867448895c88daeafbaff29.app.posit.cloud/p/32d95e56/ 1/3
9/23/24, 5:50 PM Lecture 9 Exercises
# A tibble: 58,665 × 7
year month day carrier dep_time sched_dep_time dep_delay
<int> <int> <int> <chr> <int> <int> <dbl>
1 2013 1 1 UA 517 515 2
2 2013 1 1 UA 533 529 4
3 2013 1 1 UA 554 558 -4
4 2013 1 1 UA 558 600 -2
5 2013 1 1 UA 558 600 -2
6 2013 1 1 UA 559 600 -1
7 2013 1 1 UA 607 607 0
8 2013 1 1 UA 611 600 11
9 2013 1 1 UA 623 627 -4
10 2013 1 1 UA 628 630 -2
# ℹ 58,655 more rows
# Select columns, filter for UA flights, and arrange by dep_delay in descending order
flights |>
select(year:day, carrier, contains("dep")) |>
filter(carrier == "UA") |>
arrange(desc(dep_delay))
# A tibble: 58,665 × 7
year month day carrier dep_time sched_dep_time dep_delay
<int> <int> <int> <chr> <int> <int> <dbl>
1 2013 7 26 UA 2345 1542 483
2 2013 4 18 UA 2200 1453 427
3 2013 8 28 UA 2356 1652 424
4 2013 9 26 UA 2211 1509 422
5 2013 6 25 UA 1600 900 420
6 2013 6 7 UA 2352 1659 413
7 2013 3 24 UA 2253 1605 408
8 2013 3 14 UA 1346 700 406
9 2013 5 22 UA 146 1900 406
10 2013 6 27 UA 2348 1703 405
# ℹ 58,655 more rows
Exercise 3
https://01752a68c867448895c88daeafbaff29.app.posit.cloud/p/32d95e56/ 2/3
9/23/24, 5:50 PM Lecture 9 Exercises
[1] 8255
https://01752a68c867448895c88daeafbaff29.app.posit.cloud/p/32d95e56/ 3/3