class: center, middle, inverse, title-slide # dplyr & data wrangling ### Colin Rundel ### 2018-09-12 --- exclude: true --- class: middle count: false # Pipes --- ## magrittr .middle[ .center[ <img src="imgs/magritte.jpg" width="60%" /> <img src="imgs/magrittr.jpeg" width="60%" /> ] ] --- ## Pipes in R You can think about the following sequence of actions - find key, unlock car, start car, drive to school, park. <br/> Expressed as a set of nested functions in R pseudocode this would look like: ```r park(drive(start_car(find("keys")), to="campus")) ``` <br/> Writing it out using pipes give it a more natural (and easier to read) structure: ```r find("keys") %>% start_car() %>% drive(to="campus") %>% park() ``` --- ## Approaches All of the following are fine, it comes down to personal preference: <br/> Nested: ```r h( g( f(x), y=1), z=1 ) ``` <br/> Piped: ```r f(x) %>% g(y=1) %>% h(z=1) ``` <br/> Intermediate: ```r res = f(x) res = g(res, y=1) res = h(res, z=1) ``` --- ## What about other arguments? Sometimes we want to send our results to an function argument other than first one or we want to use the previous result for multiple arguments. In these cases we can refer to the previous result using `.`. -- ```r data.frame(a=1:3,b=3:1) %>% lm(a~b,data=.) ``` ``` ## ## Call: ## lm(formula = a ~ b, data = .) ## ## Coefficients: ## (Intercept) b ## 4 -1 ``` -- ```r data.frame(a=1:3,b=3:1) %>% .[[1]] ``` ``` ## [1] 1 2 3 ``` -- ```r data.frame(a=1:3,b=3:1) %>% .[[length(.)]] ``` ``` ## [1] 3 2 1 ``` --- class: middle count: false # dplyr --- ## A Grammar of Data Manipulation dplyr is based on the concepts of functions as verbs that manipulate data frames. Single data frame functions / verbs: .small[ * `filter()`: pick rows matching criteria * `slice()`: pick rows using index(es) * `select()`: pick columns by name * `pull()`: grab a column as a vector * `rename()`: rename specific columns * `arrange()`: reorder rows * `mutate()`: add new variables * `transmute()`: create new data frame with variables * `distinct()`: filter for unique rows * `sample_n()` / `sample_frac()`: randomly sample rows * `summarise()`: reduce variables to values * ... (many more) ] --- ## dplyr rules for functions 1. First argument is *always* a data frame 2. Subsequent arguments say what to do with that data frame 3. *Always* return a data frame 4. Don't modify in place 5. Lazy evaluation magic --- ## Example Data We will demonstrate dplyr's functionality using the nycflights13 data. ```r library(dplyr) library(nycflights13) flights ``` ``` ## # A tibble: 336,776 x 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 517 515 2 830 819 ## 2 2013 1 1 533 529 4 850 830 ## 3 2013 1 1 542 540 2 923 850 ## 4 2013 1 1 544 545 -1 1004 1022 ## 5 2013 1 1 554 600 -6 812 837 ## 6 2013 1 1 554 558 -4 740 728 ## 7 2013 1 1 555 600 -5 913 854 ## 8 2013 1 1 557 600 -3 709 723 ## 9 2013 1 1 557 600 -3 838 846 ## 10 2013 1 1 558 600 -2 753 745 ## # ... with 336,766 more rows, and 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> ``` --- ## filter() - March flights ```r flights %>% filter(month == 3) ``` ``` ## # A tibble: 28,834 x 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 3 1 4 2159 125 318 56 ## 2 2013 3 1 50 2358 52 526 438 ## 3 2013 3 1 117 2245 152 223 2354 ## 4 2013 3 1 454 500 -6 633 648 ## 5 2013 3 1 505 515 -10 746 810 ## 6 2013 3 1 521 530 -9 813 827 ## 7 2013 3 1 537 540 -3 856 850 ## 8 2013 3 1 541 545 -4 1014 1023 ## 9 2013 3 1 549 600 -11 639 703 ## 10 2013 3 1 550 600 -10 747 801 ## # ... with 28,824 more rows, and 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> ``` --- ## filter() - Flights in the first 7 days of March ```r flights %>% filter(month == 3, day <= 7) ``` ``` ## # A tibble: 6,530 x 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 3 1 4 2159 125 318 56 ## 2 2013 3 1 50 2358 52 526 438 ## 3 2013 3 1 117 2245 152 223 2354 ## 4 2013 3 1 454 500 -6 633 648 ## 5 2013 3 1 505 515 -10 746 810 ## 6 2013 3 1 521 530 -9 813 827 ## 7 2013 3 1 537 540 -3 856 850 ## 8 2013 3 1 541 545 -4 1014 1023 ## 9 2013 3 1 549 600 -11 639 703 ## 10 2013 3 1 550 600 -10 747 801 ## # ... with 6,520 more rows, and 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> ``` --- ## filter() - Flights to LAX *or* RDU in March ```r flights %>% filter(dest == "LAX" | dest == "RDU", month==3) ``` ``` ## # A tibble: 1,935 x 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 3 1 607 610 -3 832 925 ## 2 2013 3 1 608 615 -7 737 750 ## 3 2013 3 1 623 630 -7 753 810 ## 4 2013 3 1 629 632 -3 844 952 ## 5 2013 3 1 657 700 -3 953 1034 ## 6 2013 3 1 714 715 -1 939 1037 ## 7 2013 3 1 716 710 6 958 1035 ## 8 2013 3 1 727 730 -3 1007 1100 ## 9 2013 3 1 803 810 -7 923 955 ## 10 2013 3 1 823 824 -1 954 1014 ## # ... with 1,925 more rows, and 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> ``` --- ## slice() - First 10 flights ```r flights %>% slice(1:10) ``` ``` ## # A tibble: 10 x 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 517 515 2 830 819 ## 2 2013 1 1 533 529 4 850 830 ## 3 2013 1 1 542 540 2 923 850 ## 4 2013 1 1 544 545 -1 1004 1022 ## 5 2013 1 1 554 600 -6 812 837 ## 6 2013 1 1 554 558 -4 740 728 ## 7 2013 1 1 555 600 -5 913 854 ## 8 2013 1 1 557 600 -3 709 723 ## 9 2013 1 1 557 600 -3 838 846 ## 10 2013 1 1 558 600 -2 753 745 ## # ... with 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> ``` --- ## slice() - Last 5 flights ```r flights %>% slice((n()-4):n()) ``` ``` ## # A tibble: 5 x 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 9 30 NA 1455 NA NA 1634 ## 2 2013 9 30 NA 2200 NA NA 2312 ## 3 2013 9 30 NA 1210 NA NA 1330 ## 4 2013 9 30 NA 1159 NA NA 1344 ## 5 2013 9 30 NA 840 NA NA 1020 ## # ... with 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> ``` --- ## select() - Individual Columns ```r flights %>% select(year, month, day) ``` ``` ## # A tibble: 336,776 x 3 ## year month day ## <int> <int> <int> ## 1 2013 1 1 ## 2 2013 1 1 ## 3 2013 1 1 ## 4 2013 1 1 ## 5 2013 1 1 ## 6 2013 1 1 ## 7 2013 1 1 ## 8 2013 1 1 ## 9 2013 1 1 ## 10 2013 1 1 ## # ... with 336,766 more rows ``` --- ## select() - Exclude Columns ```r flights %>% select(-year, -month, -day) ``` ``` ## # A tibble: 336,776 x 16 ## dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay carrier ## <int> <int> <dbl> <int> <int> <dbl> <chr> ## 1 517 515 2 830 819 11 UA ## 2 533 529 4 850 830 20 UA ## 3 542 540 2 923 850 33 AA ## 4 544 545 -1 1004 1022 -18 B6 ## 5 554 600 -6 812 837 -25 DL ## 6 554 558 -4 740 728 12 UA ## 7 555 600 -5 913 854 19 B6 ## 8 557 600 -3 709 723 -14 EV ## 9 557 600 -3 838 846 -8 B6 ## 10 558 600 -2 753 745 8 AA ## # ... with 336,766 more rows, and 9 more variables: flight <int>, ## # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>, ## # hour <dbl>, minute <dbl>, time_hour <dttm> ``` --- ## select() - Ranges ```r flights %>% select(year:day) ``` ``` ## # A tibble: 336,776 x 3 ## year month day ## <int> <int> <int> ## 1 2013 1 1 ## 2 2013 1 1 ## 3 2013 1 1 ## 4 2013 1 1 ## 5 2013 1 1 ## 6 2013 1 1 ## 7 2013 1 1 ## 8 2013 1 1 ## 9 2013 1 1 ## 10 2013 1 1 ## # ... with 336,766 more rows ``` --- ## select() - Exclusion Ranges ```r flights %>% select(-(year:day)) ``` ``` ## # A tibble: 336,776 x 16 ## dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay carrier ## <int> <int> <dbl> <int> <int> <dbl> <chr> ## 1 517 515 2 830 819 11 UA ## 2 533 529 4 850 830 20 UA ## 3 542 540 2 923 850 33 AA ## 4 544 545 -1 1004 1022 -18 B6 ## 5 554 600 -6 812 837 -25 DL ## 6 554 558 -4 740 728 12 UA ## 7 555 600 -5 913 854 19 B6 ## 8 557 600 -3 709 723 -14 EV ## 9 557 600 -3 838 846 -8 B6 ## 10 558 600 -2 753 745 8 AA ## # ... with 336,766 more rows, and 9 more variables: flight <int>, ## # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>, ## # hour <dbl>, minute <dbl>, time_hour <dttm> ``` --- class: split-50 ## select() - Matching ```r flights %>% select(contains("dep"), contains("arr")) ``` ``` ## # A tibble: 336,776 x 7 ## dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay carrier ## <int> <int> <dbl> <int> <int> <dbl> <chr> ## 1 517 515 2 830 819 11 UA ## 2 533 529 4 850 830 20 UA ## 3 542 540 2 923 850 33 AA ## 4 544 545 -1 1004 1022 -18 B6 ## 5 554 600 -6 812 837 -25 DL ## 6 554 558 -4 740 728 12 UA ## 7 555 600 -5 913 854 19 B6 ## 8 557 600 -3 709 723 -14 EV ## 9 557 600 -3 838 846 -8 B6 ## 10 558 600 -2 753 745 8 AA ## # ... with 336,766 more rows ``` --- ```r flights %>% select(starts_with("dep"), starts_with("arr")) ``` ``` ## # A tibble: 336,776 x 4 ## dep_time dep_delay arr_time arr_delay ## <int> <dbl> <int> <dbl> ## 1 517 2 830 11 ## 2 533 4 850 20 ## 3 542 2 923 33 ## 4 544 -1 1004 -18 ## 5 554 -6 812 -25 ## 6 554 -4 740 12 ## 7 555 -5 913 19 ## 8 557 -3 709 -14 ## 9 557 -3 838 -8 ## 10 558 -2 753 8 ## # ... with 336,766 more rows ``` Other helpers: `ends_with`, `matches`, `num_range`, `one_of`, `everything`, `last_col`. --- ## select_if() - Get non-numeric columns ```r flights %>% select_if(function(x) !is.numeric(x)) ``` ``` ## # A tibble: 336,776 x 5 ## carrier tailnum origin dest time_hour ## <chr> <chr> <chr> <chr> <dttm> ## 1 UA N14228 EWR IAH 2013-01-01 05:00:00 ## 2 UA N24211 LGA IAH 2013-01-01 05:00:00 ## 3 AA N619AA JFK MIA 2013-01-01 05:00:00 ## 4 B6 N804JB JFK BQN 2013-01-01 05:00:00 ## 5 DL N668DN LGA ATL 2013-01-01 06:00:00 ## 6 UA N39463 EWR ORD 2013-01-01 05:00:00 ## 7 B6 N516JB EWR FLL 2013-01-01 06:00:00 ## 8 EV N829AS LGA IAD 2013-01-01 06:00:00 ## 9 B6 N593JB JFK MCO 2013-01-01 06:00:00 ## 10 AA N3ALAA LGA ORD 2013-01-01 06:00:00 ## # ... with 336,766 more rows ``` --- ## pull() ```r names(flights) ``` ``` ## [1] "year" "month" "day" "dep_time" ## [5] "sched_dep_time" "dep_delay" "arr_time" "sched_arr_time" ## [9] "arr_delay" "carrier" "flight" "tailnum" ## [13] "origin" "dest" "air_time" "distance" ## [17] "hour" "minute" "time_hour" ``` ```r flights %>% pull("year") %>% head() ``` ``` ## [1] 2013 2013 2013 2013 2013 2013 ``` ```r flights %>% pull(1) %>% head() ``` ``` ## [1] 2013 2013 2013 2013 2013 2013 ``` ```r flights %>% pull(-1) %>% head() ``` ``` ## [1] "2013-01-01 05:00:00 EST" "2013-01-01 05:00:00 EST" ## [3] "2013-01-01 05:00:00 EST" "2013-01-01 05:00:00 EST" ## [5] "2013-01-01 06:00:00 EST" "2013-01-01 05:00:00 EST" ``` --- ## rename() - Change column names ```r flights %>% rename(tail_number = tailnum) ``` ``` ## # A tibble: 336,776 x 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 517 515 2 830 819 ## 2 2013 1 1 533 529 4 850 830 ## 3 2013 1 1 542 540 2 923 850 ## 4 2013 1 1 544 545 -1 1004 1022 ## 5 2013 1 1 554 600 -6 812 837 ## 6 2013 1 1 554 558 -4 740 728 ## 7 2013 1 1 555 600 -5 913 854 ## 8 2013 1 1 557 600 -3 709 723 ## 9 2013 1 1 557 600 -3 838 846 ## 10 2013 1 1 558 600 -2 753 745 ## # ... with 336,766 more rows, and 11 more variables: arr_delay <dbl>, ## # carrier <chr>, flight <int>, tail_number <chr>, origin <chr>, dest <chr>, ## # air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm> ``` --- ## select() vs. rename() .small[ ```r flights %>% select(tail_number = tailnum) ``` ``` ## # A tibble: 336,776 x 1 ## tail_number ## <chr> ## 1 N14228 ## 2 N24211 ## 3 N619AA ## 4 N804JB ## 5 N668DN ## 6 N39463 ## 7 N516JB ## 8 N829AS ## 9 N593JB ## 10 N3ALAA ## # ... with 336,766 more rows ``` ```r flights %>% rename(tail_number = tailnum) ``` ``` ## # A tibble: 336,776 x 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 517 515 2 830 819 ## 2 2013 1 1 533 529 4 850 830 ## 3 2013 1 1 542 540 2 923 850 ## 4 2013 1 1 544 545 -1 1004 1022 ## 5 2013 1 1 554 600 -6 812 837 ## 6 2013 1 1 554 558 -4 740 728 ## 7 2013 1 1 555 600 -5 913 854 ## 8 2013 1 1 557 600 -3 709 723 ## 9 2013 1 1 557 600 -3 838 846 ## 10 2013 1 1 558 600 -2 753 745 ## # ... with 336,766 more rows, and 11 more variables: arr_delay <dbl>, ## # carrier <chr>, flight <int>, tail_number <chr>, origin <chr>, dest <chr>, ## # air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm> ``` ] --- ## arrange() - Sort data ```r flights %>% filter(month==3,day==2) %>% arrange(origin, dest) ``` ``` ## # A tibble: 765 x 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 3 2 1336 1329 7 1426 1432 ## 2 2013 3 2 628 629 -1 837 849 ## 3 2013 3 2 637 640 -3 903 915 ## 4 2013 3 2 743 745 -2 945 1010 ## 5 2013 3 2 857 900 -3 1117 1126 ## 6 2013 3 2 1027 1030 -3 1234 1247 ## 7 2013 3 2 1134 1145 -11 1332 1359 ## 8 2013 3 2 1412 1415 -3 1636 1630 ## 9 2013 3 2 1633 1636 -3 1848 1908 ## 10 2013 3 2 1655 1700 -5 1857 1924 ## # ... with 755 more rows, and 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> ``` --- ## arrange() & desc() - Descending order ```r flights %>% filter(month==3,day==2) %>% arrange(desc(origin), dest) %>% select(origin, dest, tailnum) ``` ``` ## # A tibble: 765 x 3 ## origin dest tailnum ## <chr> <chr> <chr> ## 1 LGA ATL N928AT ## 2 LGA ATL N623DL ## 3 LGA ATL N680DA ## 4 LGA ATL N996AT ## 5 LGA ATL N510MQ ## 6 LGA ATL N663DN ## 7 LGA ATL N942DL ## 8 LGA ATL N511MQ ## 9 LGA ATL N910DE ## 10 LGA ATL N902DE ## # ... with 755 more rows ``` --- ## mutate() - Modify columns ```r flights %>% select(year:day) %>% mutate(date = paste(year,month,day,sep="/")) ``` ``` ## # A tibble: 336,776 x 4 ## year month day date ## <int> <int> <int> <chr> ## 1 2013 1 1 2013/1/1 ## 2 2013 1 1 2013/1/1 ## 3 2013 1 1 2013/1/1 ## 4 2013 1 1 2013/1/1 ## 5 2013 1 1 2013/1/1 ## 6 2013 1 1 2013/1/1 ## 7 2013 1 1 2013/1/1 ## 8 2013 1 1 2013/1/1 ## 9 2013 1 1 2013/1/1 ## 10 2013 1 1 2013/1/1 ## # ... with 336,766 more rows ``` --- ## transmute() - Create new tibble from existing columns ```r flights %>% select(year:day) %>% transmute(date = paste(year,month,day,sep="/")) ``` ``` ## # A tibble: 336,776 x 1 ## date ## <chr> ## 1 2013/1/1 ## 2 2013/1/1 ## 3 2013/1/1 ## 4 2013/1/1 ## 5 2013/1/1 ## 6 2013/1/1 ## 7 2013/1/1 ## 8 2013/1/1 ## 9 2013/1/1 ## 10 2013/1/1 ## # ... with 336,766 more rows ``` --- ## distinct() - Find unique rows ```r flights %>% select(origin, dest) %>% distinct() %>% arrange(origin,dest) ``` ``` ## # A tibble: 224 x 2 ## origin dest ## <chr> <chr> ## 1 EWR ALB ## 2 EWR ANC ## 3 EWR ATL ## 4 EWR AUS ## 5 EWR AVL ## 6 EWR BDL ## 7 EWR BNA ## 8 EWR BOS ## 9 EWR BQN ## 10 EWR BTV ## # ... with 214 more rows ``` --- ## Sampling rows .small[ ```r flights %>% sample_n(10) ``` ``` ## # A tibble: 10 x 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 7 27 1243 1139 64 1347 1254 ## 2 2013 1 13 1726 1508 138 1920 1705 ## 3 2013 3 13 1532 1523 9 1631 1636 ## 4 2013 5 26 1411 1414 -3 1530 1542 ## 5 2013 4 3 1914 1925 -11 2216 2243 ## 6 2013 3 8 1707 1645 22 1953 2008 ## 7 2013 11 25 653 700 -7 922 936 ## 8 2013 9 23 1657 1659 -2 1857 1905 ## 9 2013 12 29 1835 1830 5 2135 2124 ## 10 2013 9 23 1928 1830 58 2225 2157 ## # ... with 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> ``` ```r flights %>% sample_frac(0.00003) ``` ``` ## # A tibble: 10 x 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 10 24 635 640 -5 755 807 ## 2 2013 1 18 1946 1940 6 2043 2106 ## 3 2013 6 3 702 700 2 1004 1010 ## 4 2013 4 26 1202 1200 2 1520 1510 ## 5 2013 12 6 2007 1830 97 2155 2046 ## 6 2013 1 6 1426 1430 -4 1733 1755 ## 7 2013 6 6 1431 1440 -9 1612 1630 ## 8 2013 7 18 1050 1055 -5 1311 1347 ## 9 2013 5 4 746 750 -4 1134 1155 ## 10 2013 8 16 1614 1610 4 1906 1858 ## # ... with 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> ``` ] --- ## summarise() ```r flights %>% summarize(n(), min(dep_delay), max(dep_delay)) ``` ``` ## # A tibble: 1 x 3 ## `n()` `min(dep_delay)` `max(dep_delay)` ## <int> <dbl> <dbl> ## 1 336776 NA NA ``` -- ```r flights %>% summarize( n = n(), min_dep_delay = min(dep_delay, na.rm = TRUE), max_dep_delay = max(dep_delay, na.rm = TRUE) ) ``` ``` ## # A tibble: 1 x 3 ## n min_dep_delay max_dep_delay ## <int> <dbl> <dbl> ## 1 336776 -43 1301 ``` --- ## group_by() ```r flights %>% group_by(origin) ``` ``` ## # A tibble: 336,776 x 19 ## # Groups: origin [3] ## 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 517 515 2 830 819 ## 2 2013 1 1 533 529 4 850 830 ## 3 2013 1 1 542 540 2 923 850 ## 4 2013 1 1 544 545 -1 1004 1022 ## 5 2013 1 1 554 600 -6 812 837 ## 6 2013 1 1 554 558 -4 740 728 ## 7 2013 1 1 555 600 -5 913 854 ## 8 2013 1 1 557 600 -3 709 723 ## 9 2013 1 1 557 600 -3 838 846 ## 10 2013 1 1 558 600 -2 753 745 ## # ... with 336,766 more rows, and 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> ``` --- ## summarise() with group_by() ```r flights %>% group_by(origin) %>% summarize( n = n(), min_dep_delay = min(dep_delay, na.rm = TRUE), max_dep_delay = max(dep_delay, na.rm = TRUE) ) ``` ``` ## # A tibble: 3 x 4 ## origin n min_dep_delay max_dep_delay ## <chr> <int> <dbl> <dbl> ## 1 EWR 120835 -25 1126 ## 2 JFK 111279 -43 1301 ## 3 LGA 104662 -33 911 ``` --- ```r flights %>% group_by(origin, carrier) %>% summarize( n = n(), min_dep_delay = min(dep_delay, na.rm = TRUE), max_dep_delay = max(dep_delay, na.rm = TRUE) ) %>% filter(n > 10000) ``` ``` ## # A tibble: 10 x 5 ## # Groups: origin [3] ## origin carrier n min_dep_delay max_dep_delay ## <chr> <chr> <int> <dbl> <dbl> ## 1 EWR EV 43939 -25 548 ## 2 EWR UA 46087 -18 424 ## 3 JFK 9E 14651 -24 747 ## 4 JFK AA 13783 -15 1014 ## 5 JFK B6 42076 -43 453 ## 6 JFK DL 20701 -18 960 ## 7 LGA AA 15459 -24 803 ## 8 LGA DL 23067 -33 911 ## 9 LGA MQ 16928 -26 366 ## 10 LGA US 13136 -18 500 ``` --- ## mutate() with group_by() ```r flights %>% group_by(origin) %>% mutate( n = n(), ) %>% select(origin, n) ``` ``` ## # A tibble: 336,776 x 2 ## # Groups: origin [3] ## origin n ## <chr> <int> ## 1 EWR 120835 ## 2 LGA 104662 ## 3 JFK 111279 ## 4 JFK 111279 ## 5 LGA 104662 ## 6 EWR 120835 ## 7 EWR 120835 ## 8 LGA 104662 ## 9 JFK 111279 ## 10 LGA 104662 ## # ... with 336,766 more rows ``` --- ## Demos 1. How many flights to Los Angeles (LAX) did each of the legacy carriers (AA, UA, DL or US) have in May from JFK, and what was their average duration? <br/> 1. What was the shortest flight out of each airport in terms of distance? In terms of duration? --- ## Exercises 1. Which plane (check the tail number) flew out of each New York airport the most? <br/> 1. Which date should you fly on if you want to have the lowest possible average departure delay? What about arrival delay?