2  Assignment 2

Author

Sabri Demirdal

Published

January 8, 2023

2.1 Install packages

library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.4.0      ✔ 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 
Warning: package 'ggplot2' was built under R version 4.2.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
library(nycflights13)
Warning: package 'nycflights13' was built under R version 4.2.2

2.2 Exploratory data analysis with dplyr

2.2.1 Inspect the data

df_planes <- planes
head(df_planes,10)
# A tibble: 10 × 9
   tailnum  year type                   manuf…¹ model engines seats speed engine
   <chr>   <int> <chr>                  <chr>   <chr>   <int> <int> <int> <chr> 
 1 N10156   2004 Fixed wing multi engi… EMBRAER EMB-…       2    55    NA Turbo…
 2 N102UW   1998 Fixed wing multi engi… AIRBUS… A320…       2   182    NA Turbo…
 3 N103US   1999 Fixed wing multi engi… AIRBUS… A320…       2   182    NA Turbo…
 4 N104UW   1999 Fixed wing multi engi… AIRBUS… A320…       2   182    NA Turbo…
 5 N10575   2002 Fixed wing multi engi… EMBRAER EMB-…       2    55    NA Turbo…
 6 N105UW   1999 Fixed wing multi engi… AIRBUS… A320…       2   182    NA Turbo…
 7 N107US   1999 Fixed wing multi engi… AIRBUS… A320…       2   182    NA Turbo…
 8 N108UW   1999 Fixed wing multi engi… AIRBUS… A320…       2   182    NA Turbo…
 9 N109UW   1999 Fixed wing multi engi… AIRBUS… A320…       2   182    NA Turbo…
10 N110UW   1999 Fixed wing multi engi… AIRBUS… A320…       2   182    NA Turbo…
# … with abbreviated variable name ¹​manufacturer
df_planes %>%  glimpse()
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.2.2 Rank Total number of planes produced by the manufacturers according to their type after the 2000.

df_planes %>% filter(year >= 2000) %>% group_by(manufacturer) %>% count(type) %>% arrange(desc(n)) %>% 
  mutate(number_of_planes=n)
# A tibble: 10 × 4
# Groups:   manufacturer [10]
   manufacturer           type                         n number_of_planes
   <chr>                  <chr>                    <int>            <int>
 1 BOEING                 Fixed wing multi engine    896              896
 2 BOMBARDIER INC         Fixed wing multi engine    356              356
 3 AIRBUS                 Fixed wing multi engine    328              328
 4 EMBRAER                Fixed wing multi engine    260              260
 5 AIRBUS INDUSTRIE       Fixed wing multi engine    180              180
 6 AGUSTA SPA             Rotorcraft                   1                1
 7 AVIAT AIRCRAFT INC     Fixed wing single engine     1                1
 8 CIRRUS DESIGN CORP     Fixed wing single engine     1                1
 9 FRIEDEMANN JON         Fixed wing single engine     1                1
10 ROBINSON HELICOPTER CO Rotorcraft                   1                1

2.2.3 Find the years that the total number of seats in the planes produced by AIRBUS is more than BOEING

AIRBUS_seat_number<-df_planes %>% filter(manufacturer=="AIRBUS") %>% group_by(year) %>% summarise(total_number_of_seats=sum(seats))
BOEING_seat_number<-df_planes %>% filter(manufacturer=="BOEING") %>% group_by(year) %>% summarise(total_number_of_seats=sum(seats))
AIRBUS_seat_number$year[AIRBUS_seat_number$total_number_of_seats>BOEING_seat_number$total_number_of_seats]
Warning in AIRBUS_seat_number$total_number_of_seats >
BOEING_seat_number$total_number_of_seats: uzun olan nesne uzunluğu kısa olan
nesne uzunluğunun bir katı değil
 [1] 2002 2003 2004 2005 2006 2008 2012 2013   NA   NA   NA   NA   NA   NA