2  In Class Exercise 1

Author

Ali Gökmen

Published

October 21, 2022

Let’s invoke the libraries we need in this exercise.

library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.3.6      ✔ purrr   0.3.5 
✔ tibble  3.1.8      ✔ dplyr   1.0.10
✔ tidyr   1.2.1      ✔ stringr 1.4.1 
✔ readr   2.1.3      ✔ forcats 0.5.2 
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
library(nycflights13)

Before analysis, let’s take a look what we have in the dataset.

glimpse(planes)
Rows: 3,322
Columns: 9
$ tailnum      <chr> "N10156", "N102UW", "N103US", "N104UW", "N10575", "N105UW…
$ year         <int> 2004, 1998, 1999, 1999, 2002, 1999, 1999, 1999, 1999, 199…
$ type         <chr> "Fixed wing multi engine", "Fixed wing multi engine", "Fi…
$ manufacturer <chr> "EMBRAER", "AIRBUS INDUSTRIE", "AIRBUS INDUSTRIE", "AIRBU…
$ model        <chr> "EMB-145XR", "A320-214", "A320-214", "A320-214", "EMB-145…
$ engines      <int> 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, …
$ seats        <int> 55, 182, 182, 182, 55, 182, 182, 182, 182, 182, 55, 55, 5…
$ speed        <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ engine       <chr> "Turbo-fan", "Turbo-fan", "Turbo-fan", "Turbo-fan", "Turb…

2.1 The number of planes having Turbo-jet engine by manufacturer

planes%>%
  filter(engine == "Turbo-jet")%>%
  group_by(manufacturer,model,engine)%>%
  summarize(count=n())%>%
  arrange(desc(count))
`summarise()` has grouped output by 'manufacturer', 'model'. You can override
using the `.groups` argument.
# A tibble: 41 × 4
# Groups:   manufacturer, model [41]
   manufacturer      model    engine    count
   <chr>             <chr>    <chr>     <int>
 1 BOEING            757-222  Turbo-jet    80
 2 BOEING            737-832  Turbo-jet    71
 3 AIRBUS INDUSTRIE  A319-131 Turbo-jet    50
 4 BOEING            757-224  Turbo-jet    41
 5 BOEING            737-724  Turbo-jet    32
 6 BOEING            757-251  Turbo-jet    29
 7 AIRBUS INDUSTRIE  A320-211 Turbo-jet    25
 8 AIRBUS INDUSTRIE  A321-211 Turbo-jet    25
 9 MCDONNELL DOUGLAS MD-90-30 Turbo-jet    21
10 AIRBUS INDUSTRIE  A319-132 Turbo-jet    20
# … with 31 more rows

2.2 The number of planes with age and manufacturer detail

planes%>%
  group_by(manufacturer,model) %>%
  summarize(count=n(),avg_age = mean(2022-year, na.rm = T))%>%
  arrange(desc(count), avg_age)
`summarise()` has grouped output by 'manufacturer'. You can override using the
`.groups` argument.
# A tibble: 147 × 4
# Groups:   manufacturer [35]
   manufacturer                  model       count avg_age
   <chr>                         <chr>       <int>   <dbl>
 1 BOEING                        737-7H4       361    18.1
 2 BOMBARDIER INC                CL-600-2B19   162    19.3
 3 AIRBUS                        A320-232      129    16.3
 4 AIRBUS INDUSTRIE              A320-232      127    23.9
 5 BOMBARDIER INC                CL-600-2D24   123    14.9
 6 BOEING                        737-824       122    19.4
 7 EMBRAER                       EMB-145LR     114    21.6
 8 BOEING                        737-3H4       105    27.6
 9 EMBRAER                       EMB-145XR     104    18.4
10 MCDONNELL DOUGLAS AIRCRAFT CO MD-88         103    32.3
# … with 137 more rows