Use the flights dataframe in the following answers:
use a code chunk to:
glimpse(flights)
## Rows: 336,776
## Columns: 19
## $ year <int> 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2…
## $ month <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ day <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ dep_time <int> 517, 533, 542, 544, 554, 554, 555, 557, 557, 558, 558, …
## $ sched_dep_time <int> 515, 529, 540, 545, 600, 558, 600, 600, 600, 600, 600, …
## $ dep_delay <dbl> 2, 4, 2, -1, -6, -4, -5, -3, -3, -2, -2, -2, -2, -2, -1…
## $ arr_time <int> 830, 850, 923, 1004, 812, 740, 913, 709, 838, 753, 849,…
## $ sched_arr_time <int> 819, 830, 850, 1022, 837, 728, 854, 723, 846, 745, 851,…
## $ arr_delay <dbl> 11, 20, 33, -18, -25, 12, 19, -14, -8, 8, -2, -3, 7, -1…
## $ carrier <chr> "UA", "UA", "AA", "B6", "DL", "UA", "B6", "EV", "B6", "…
## $ flight <int> 1545, 1714, 1141, 725, 461, 1696, 507, 5708, 79, 301, 4…
## $ tailnum <chr> "N14228", "N24211", "N619AA", "N804JB", "N668DN", "N394…
## $ origin <chr> "EWR", "LGA", "JFK", "JFK", "LGA", "EWR", "EWR", "LGA",…
## $ dest <chr> "IAH", "IAH", "MIA", "BQN", "ATL", "ORD", "FLL", "IAD",…
## $ air_time <dbl> 227, 227, 160, 183, 116, 150, 158, 53, 140, 138, 149, 1…
## $ distance <dbl> 1400, 1416, 1089, 1576, 762, 719, 1065, 229, 944, 733, …
## $ hour <dbl> 5, 5, 5, 5, 6, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 6, 6, 6…
## $ minute <dbl> 15, 29, 40, 45, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 59, 0…
## $ time_hour <dttm> 2013-01-01 05:00:00, 2013-01-01 05:00:00, 2013-01-01 0…
use a code chunk to:
J1stflights<-subset(flights, month==1 & day==1)
nrow(J1stflights)
## [1] 842
How many flights had a departure delay of over 60 minutes, but only an arrival delay of less than 30 minutes?
efficientflights<-subset(flights, dep_delay>60 & arr_delay<30)
nrow(efficientflights)
## [1] 181
Use arrange to help identify the 10 flights with the
longest arrival delays. Display only the carrier, arrival delay, and
tail number
lateflights <- select(flights, carrier, arr_delay, tailnum)
lateflights <- arrange(lateflights, desc(arr_delay))
print(lateflights)
## # A tibble: 336,776 × 3
## carrier arr_delay tailnum
## <chr> <dbl> <chr>
## 1 HA 1272 N384HA
## 2 MQ 1127 N504MQ
## 3 MQ 1109 N517MQ
## 4 AA 1007 N338AA
## 5 MQ 989 N665MQ
## 6 DL 931 N959DL
## 7 DL 915 N927DA
## 8 DL 895 N6716C
## 9 AA 878 N5DMAA
## 10 MQ 875 N523MQ
## # ℹ 336,766 more rows
use a code chunk to answer the following:
From the original dataframe, create a new dataframe to determine how many flights where the arrival delay was > 60 minutes. How many flights were there?
verylateflights <- subset(flights, arr_delay>=60)
nrow(verylateflights)
## [1] 28317
Note the the following 2 letter airline carrier codes can be retrieved by the following
print(airlines)
## # A tibble: 16 × 2
## carrier name
## <chr> <chr>
## 1 9E Endeavor Air Inc.
## 2 AA American Airlines Inc.
## 3 AS Alaska Airlines Inc.
## 4 B6 JetBlue Airways
## 5 DL Delta Air Lines Inc.
## 6 EV ExpressJet Airlines Inc.
## 7 F9 Frontier Airlines Inc.
## 8 FL AirTran Airways Corporation
## 9 HA Hawaiian Airlines Inc.
## 10 MQ Envoy Air
## 11 OO SkyWest Airlines Inc.
## 12 UA United Air Lines Inc.
## 13 US US Airways Inc.
## 14 VX Virgin America
## 15 WN Southwest Airlines Co.
## 16 YV Mesa Airlines Inc.
use a code chunk to determine which carrier had the most flights with arrival delays > 60 minutes. hint: use a table
prettylateflights = table(filter(flights, arr_delay > 60)$carrier)
sort(prettylateflights, decreasing = TRUE)
##
## EV B6 UA DL MQ AA 9E WN US VX FL F9 YV AS HA OO
## 6803 4965 3931 2927 2323 2070 1830 1063 937 374 360 87 74 33 8 4
For all carriers that have the following 2 letter carrier names, AA, DL, EV, MQ, UA, WN, display a bar chart of the number of late arrival flights > 60 minutes. Make sure that labels and titles are included.
hint: The following code will create a data frame appropriate for a bar chart.
selected_carriers <- c("AA", "DL", "EV", "MQ", "UA", "WN")
late_flights <- subset(flights, carrier %in% selected_carriers & arr_delay > 60)
delay_counts <- as.data.frame(table(late_flights$carrier))
ggplot(delay_counts, aes(x = Var1, y = Freq, fill = Var1))+
geom_bar(stat = "identity") +
labs(title = "Late Flights vs Carrier",
x = "Carrier",
y = "Flight Number")
We’ve identified that EV has the most issues with delayed flights. Visualize the number of delayed flights over the year (aggregated by month) with a line chart. The following code will create a data frame appropriate for a line chart._
# Subset for EV carrier and delays
ev_delays <- subset(flights, carrier == "EV" & arr_delay > 60)
# Create a date column
ev_delays$date <- as.Date(with(ev_delays, paste(year, month, day, sep = "-")))
# Create a month column (first day of each month)
ev_delays$month <- floor_date(ev_delays$date, unit = "month")
# Count delayed flights per month
monthly_counts <- ev_delays %>%
count(month)
ggplot(monthly_counts, aes(x = month, y = n)) +
geom_line(color = "green") +
geom_point() +
labs(title = "Monthly Number of EV Delayed Flights",
x = "Month",
y = "Number of Delayed Flights")
How many total flights did the carrier EV operate in 2013? What percentage of flights were the arrival delayed by more than 60 minutes?
evflights = filter(flights, carrier == "EV")
nrow(evflights)
## [1] 54173
lateevflights = filter(evflights, arr_delay > 60)
nrow(lateevflights)
## [1] 6803
lateevflightpercentage = (nrow(lateevflights) / nrow(evflights)) * 100
print(lateevflightpercentage)
## [1] 12.55792
Come up with an interesting question about the data in data frame. Write your question as well as the R code and results that answers your question. How many ev flights happened in September?
septevflights <- subset(evflights, month == 9)
nrow(septevflights)
## [1] 4725