Load the appropriate libraries:
library(nycflights13)
library(tidyr)
library(conflicted)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ purrr 1.0.2
## ✔ forcats 1.0.0 ✔ readr 2.1.5
## ✔ ggplot2 3.5.1 ✔ stringr 1.5.1
## ✔ lubridate 1.9.3 ✔ tibble 3.2.1
library(dplyr)
conflicts_prefer(dplyr::filter)
## [conflicted] Will prefer dplyr::filter over any other package.
We are looking for flights in May with the origin from JFK and destination LAX. It was easier to first grab all the flights and the destinations, before I then filtered out the airlines that I wanted. Then I selected for the carriers and air time and finally just calculated the average.
carrier_table = flights %>% filter(month == 5, origin == "JFK")
carrier_table = carrier_table %>% filter(dest == "LAX")
carrier_table = carrier_table %>% filter(carrier == "AA" | carrier == "UA"
| carrier == "DL" | carrier == "US")
carrier_table = carrier_table %>% select(carrier, air_time)
carrier_table = carrier_table %>% group_by(carrier) %>%
summarise(n = n(), avg_flight_time = mean(air_time, na.rm = TRUE))
carrier_table = carrier_table %>% rename("Number of Flights" = n,
"Average Flight Time in Minutes" =
avg_flight_time)
carrier_table
This question is looking for the shortest flight in terms of distance and duration that came out of each airport. I interpreted that as the origin.
flights %>% select(origin, distance, air_time) %>% group_by(origin) %>%
summarise(min_distance = min(distance, na.rm = TRUE),
min_duration = min(air_time, na.rm = TRUE))
I did each airport at a time to get the max for each flight. I created a function so I can get the index where the max number of flights for a plane is located in the table.
## function to find the max index
find_flight = function(flights){
max_flight = max(flights)
i = 1
while (i <= length(flights)){
if (flights[i] == max_flight){
return(i)
}
i = i + 1
}
}
First EWR airport max flights for a plane
ewr_planes = flights %>% filter(origin == "EWR", !is.na(tailnum)) %>%
select(origin, tailnum) %>% group_by(origin, tailnum) %>% summarise(n = n())
## `summarise()` has grouped output by 'origin'. You can override using the
## `.groups` argument.
ewr_planes = ewr_planes %>% rename("Number of Flights" = n)
ewr_planes
Then to find the max number of flights for a plane from EWR
ewr_index = find_flight(ewr_planes$`Number of Flights`)
ewr_planes[ewr_index,]
For JFK:
## For JFK
jfk_planes = flights %>% filter(origin == "JFK", !is.na(tailnum)) %>%
select(origin, tailnum) %>% group_by(origin, tailnum) %>% summarise(n = n())
## `summarise()` has grouped output by 'origin'. You can override using the
## `.groups` argument.
jfk_planes = jfk_planes %>% rename("Number of Flights" = n)
jfk_planes
jfk_index = find_flight(jfk_planes$`Number of Flights`)
jfk_planes[jfk_index,]
For LGA:
## For LGA
lga_planes = flights %>% filter(origin == "LGA", !is.na(tailnum)) %>%
select(origin, tailnum) %>% group_by(origin, tailnum) %>% summarise(n = n())
## `summarise()` has grouped output by 'origin'. You can override using the
## `.groups` argument.
lga_planes = lga_planes %>% rename("Number of Flights" = n)
lga_planes
lga_index = find_flight(lga_planes$`Number of Flights`)
lga_planes[lga_index,]
This is looking for which day had the best departure and arrival time on avg. I grouped the dat together by each day and look at the average departing and arival delays. Found the index for the min of the average and that gave me the best day out of 2013.
avg_delay = flights %>% filter(!is.na(dep_delay), !is.na(arr_delay)) %>%
select(year, month, day, dep_delay, arr_delay)
avg_delay = avg_delay %>% group_by(year, month, day) %>%
summarise(avg_dep_delay = mean(dep_delay), avg_arr_delay = mean(arr_delay))
## `summarise()` has grouped output by 'year', 'month'. You can override using the
## `.groups` argument.
avg_delay
Function to find the min index
find_min_index = function(delay){
i = 1
min_delay = min(delay)
while (i <= length(delay)) {
if (delay[i] == min_delay) {
return(i)
}
i = i + 1
}
}
Best day with minimal departing delays
dep_delay_index = find_min_index(avg_delay$avg_dep_delay)
avg_delay[dep_delay_index,]
Best day with minimal arrival delays
arr_delay_index = find_min_index(avg_delay$avg_arr_delay)
avg_delay[arr_delay_index,]