NYC Flights Assignment

Author

Girma Wakene

Load the Library

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(nycflights23)
data("flights")

get a close look data

this helps me to see all data closely to identify which variables are most important to my selection.

flights
# A tibble: 435,352 × 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  2023     1     1        1           2038       203      328              3
 2  2023     1     1       18           2300        78      228            135
 3  2023     1     1       31           2344        47      500            426
 4  2023     1     1       33           2140       173      238           2352
 5  2023     1     1       36           2048       228      223           2252
 6  2023     1     1      503            500         3      808            815
 7  2023     1     1      520            510        10      948            949
 8  2023     1     1      524            530        -6      645            710
 9  2023     1     1      537            520        17      926            818
10  2023     1     1      547            545         2      845            852
# ℹ 435,342 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>

summary of the flights

this summary data gives clear picture of the data from the data I choose 11 variable by sellect dplyre.
summary(flights)
      year          month             day           dep_time     sched_dep_time
 Min.   :2023   Min.   : 1.000   Min.   : 1.00   Min.   :   1    Min.   : 500  
 1st Qu.:2023   1st Qu.: 3.000   1st Qu.: 8.00   1st Qu.: 931    1st Qu.: 930  
 Median :2023   Median : 6.000   Median :16.00   Median :1357    Median :1359  
 Mean   :2023   Mean   : 6.423   Mean   :15.74   Mean   :1366    Mean   :1364  
 3rd Qu.:2023   3rd Qu.: 9.000   3rd Qu.:23.00   3rd Qu.:1804    3rd Qu.:1759  
 Max.   :2023   Max.   :12.000   Max.   :31.00   Max.   :2400    Max.   :2359  
                                                 NA's   :10738                 
   dep_delay          arr_time     sched_arr_time   arr_delay       
 Min.   : -50.00   Min.   :   1    Min.   :   1   Min.   : -97.000  
 1st Qu.:  -6.00   1st Qu.:1105    1st Qu.:1135   1st Qu.: -22.000  
 Median :  -2.00   Median :1519    Median :1551   Median : -10.000  
 Mean   :  13.84   Mean   :1497    Mean   :1552   Mean   :   4.345  
 3rd Qu.:  10.00   3rd Qu.:1946    3rd Qu.:2007   3rd Qu.:   9.000  
 Max.   :1813.00   Max.   :2400    Max.   :2359   Max.   :1812.000  
 NA's   :10738     NA's   :11453                  NA's   :12534     
   carrier              flight         tailnum             origin         
 Length:435352      Min.   :   1.0   Length:435352      Length:435352     
 Class :character   1st Qu.: 364.0   Class :character   Class :character  
 Mode  :character   Median : 734.0   Mode  :character   Mode  :character  
                    Mean   : 785.2                                        
                    3rd Qu.:1188.0                                        
                    Max.   :1972.0                                        
                                                                          
     dest              air_time        distance           hour      
 Length:435352      Min.   : 18.0   Min.   :  80.0   Min.   : 5.00  
 Class :character   1st Qu.: 77.0   1st Qu.: 479.0   1st Qu.: 9.00  
 Mode  :character   Median :121.0   Median : 762.0   Median :13.00  
                    Mean   :141.8   Mean   : 977.5   Mean   :13.35  
                    3rd Qu.:177.0   3rd Qu.:1182.0   3rd Qu.:17.00  
                    Max.   :701.0   Max.   :4983.0   Max.   :23.00  
                    NA's   :12534                                   
     minute        time_hour                     
 Min.   : 0.00   Min.   :2023-01-01 05:00:00.00  
 1st Qu.:10.00   1st Qu.:2023-03-30 20:00:00.00  
 Median :29.00   Median :2023-06-27 08:00:00.00  
 Mean   :28.53   Mean   :2023-06-29 10:02:22.39  
 3rd Qu.:45.00   3rd Qu.:2023-09-27 11:00:00.00  
 Max.   :59.00   Max.   :2023-12-31 23:00:00.00  
                                                 

Select to minimize the variables

I select only 11 variables by dplyr from 19 choose only 11 variable

library(dplyr)
flights2 <- flights |>
  select(year, month, dep_time, dep_delay, arr_time, arr_delay, carrier, tailnum, origin, dest, arr_time,time_hour ) |>
  group_by(month, carrier)
head(flights2)
# A tibble: 6 × 11
# Groups:   month, carrier [4]
   year month dep_time dep_delay arr_time arr_delay carrier tailnum origin dest 
  <int> <int>    <int>     <dbl>    <int>     <dbl> <chr>   <chr>   <chr>  <chr>
1  2023     1        1       203      328       205 UA      N25201  EWR    SMF  
2  2023     1       18        78      228        53 DL      N830DN  JFK    ATL  
3  2023     1       31        47      500        34 B6      N807JB  JFK    BQN  
4  2023     1       33       173      238       166 B6      N265JB  JFK    CHS  
5  2023     1       36       228      223       211 UA      N17730  EWR    DTW  
6  2023     1      503         3      808        -7 AA      N925AN  EWR    MIA  
# ℹ 1 more variable: time_hour <dttm>

Group and summarize data

Calculate the total flights by day by grop_by.

# Calculate total flights by day
day_sum <- flights |>
  group_by(month, carrier) |>
  summarize(total_flights = n()) |>
  arrange(desc(total_flights))  ## total_flights is a new variable created
`summarise()` has grouped output by 'month'. You can override using the
`.groups` argument.
day_sum
# A tibble: 165 × 3
# Groups:   month [12]
   month carrier total_flights
   <int> <chr>           <int>
 1     3 YX               8384
 2     5 YX               8320
 3     4 YX               8125
 4     1 YX               7657
 5     9 YX               7626
 6     2 YX               7557
 7     8 YX               7468
 8     7 YX               7308
 9     3 UA               7243
10    10 YX               7165
# ℹ 155 more rows

using ggplot to do a bar graph

using plot 1 to make a bar graph that shows the x and y axes to show the carrier list and flight.

plot1 <- flights |>
  ggplot() +
  geom_bar(aes(x=flight, y=carrier,fill = origin ),
      position = "dodge", stat = "identity") +
  labs(fill = "total flights",
       y = "carriers list",
       title = "NYCflights 2023",
       caption = "Source:RITA, Bureau of transportation statistics, https://www.transtats.bts.gov/DL_SelectFields.asp?Table_ID=236")
plot1

description of the bar graph

This bar graph represents the NYC flight in 2023 which starts from NYC domestic flights the destinations of different local destinations of the United States. The X-axis represents the number of flights each carrier takes throughout the year. The Y-axis represents each carrier that is serviced in three NYC state airports. The flight data shows all three airports of NYC listed EWR, JFK, and LGA.

plot1 <- flights |>
  ggplot() +
  geom_bar(aes(x=origin, y=flight, fill = carrier ),
      position = "dodge", stat = "identity") +
  labs(fill = "carriers",
       y = "number of flight",
       title = "NYC flights 2023",
       caption = "Source:RITA, Bureau of transportation statistics, https://www.transtats.bts.gov/DL_SelectFields.asp?Table_ID=236")
plot1

description of the bar graph

This bar graph represents the NYC flight in 2023 which starts from NYC domestic flights the destinations of different local destinations of the United States. The X-axis represents the origin of the flight of three airports. The Y-axis represents the number of flights for each airport. The flight data shows all fourteen carriers that serve in NYC airports.