2  In Class Exercise 1

Published

October 19, 2022

library(tidyverse)
library(nycflights13)
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.1 The number of planes according to their manufacturers.

planes %>% 
  group_by(manufacturer) %>% 
  summarise(count=n()) %>% 
  arrange(manufacturer) %>% 
  print(n=35)
# A tibble: 35 × 2
   manufacturer                  count
   <chr>                         <int>
 1 AGUSTA SPA                        1
 2 AIRBUS                          336
 3 AIRBUS INDUSTRIE                400
 4 AMERICAN AIRCRAFT INC             2
 5 AVIAT AIRCRAFT INC                1
 6 AVIONS MARCEL DASSAULT            1
 7 BARKER JACK L                     1
 8 BEECH                             2
 9 BELL                              2
10 BOEING                         1630
11 BOMBARDIER INC                  368
12 CANADAIR                          9
13 CANADAIR LTD                      1
14 CESSNA                            9
15 CIRRUS DESIGN CORP                1
16 DEHAVILLAND                       1
17 DOUGLAS                           1
18 EMBRAER                         299
19 FRIEDEMANN JON                    1
20 GULFSTREAM AEROSPACE              2
21 HURLEY JAMES LARRY                1
22 JOHN G HESS                       1
23 KILDALL GARY                      1
24 LAMBERT RICHARD                   1
25 LEARJET INC                       1
26 LEBLANC GLENN T                   1
27 MARZ BARRY                        1
28 MCDONNELL DOUGLAS               120
29 MCDONNELL DOUGLAS AIRCRAFT CO   103
30 MCDONNELL DOUGLAS CORPORATION    14
31 PAIR MIKE E                       1
32 PIPER                             5
33 ROBINSON HELICOPTER CO            1
34 SIKORSKY                          1
35 STEWART MACO                      2

2.2 Max engine numbers group by years.

planes %>%
  group_by(year) %>%
  filter(duplicated(year)) %>%
  arrange(year) %>%
  mutate("max_engine_num"=max(engines)) %>%
  summarise(max_engine_num=paste0(unique(max_engine_num),collapse=',')) %>%
  print(n=39)
# A tibble: 39 × 2
    year max_engine_num
   <int> <chr>         
 1  1959 1             
 2  1963 1             
 3  1975 2             
 4  1976 2             
 5  1977 2             
 6  1978 2             
 7  1979 2             
 8  1980 2             
 9  1984 2             
10  1985 2             
11  1986 3             
12  1987 2             
13  1988 2             
14  1989 2             
15  1990 4             
16  1991 2             
17  1992 2             
18  1993 2             
19  1994 2             
20  1995 2             
21  1996 2             
22  1997 2             
23  1998 2             
24  1999 2             
25  2000 2             
26  2001 2             
27  2002 2             
28  2003 2             
29  2004 3             
30  2005 2             
31  2006 2             
32  2007 2             
33  2008 2             
34  2009 2             
35  2010 2             
36  2011 2             
37  2012 2             
38  2013 2             
39    NA 4             

2.3 Total plane count per engines.

planes %>%
  group_by(engines) %>%
  mutate("average_engine_nums"=mean(engines, 1)) %>%
  count(average_engine_nums)
# A tibble: 4 × 3
# Groups:   engines [4]
  engines average_engine_nums     n
    <int>               <dbl> <int>
1       1                   1    27
2       2                   2  3288
3       3                   3     3
4       4                   4     4