##Load the CSV file FlightsWithAirlines.csv containing into a data frame called df.flights. Do not load the text (strings) attributes (columns) as factors, so use stringsAsFactors = FALSE as a parameter in your function that loads the data; load them as text. Load the file from the URL rather than downloading the file to your computer. To get the URL for the CSV, right-click on the link and then select “Copy Link Address” or a similar menu option for your browser; do not click on the link as that will cause the browser to attempt to download and display the file.##

df.flights <- read.csv ("FlightsWithAirlines.csv")

##Use the R function str() to understand the structure of the data frame.##

str(df.flights)
## 'data.frame':    18 obs. of  14 variables:
##  $ year     : int  2022 2022 2022 2022 2022 2022 2022 2022 2022 2022 ...
##  $ month    : int  9 2 1 6 1 5 5 10 6 8 ...
##  $ day      : int  18 14 7 7 30 2 5 24 16 18 ...
##  $ dep_hr   : int  2 12 13 22 24 11 5 20 5 2 ...
##  $ dep_min  : int  38 25 56 56 2 2 50 23 31 38 ...
##  $ dep_delay: int  0 13 9 7 14 49 13 50 30 20 ...
##  $ carrier  : chr  "UA" "AA" "AA" "B6" ...
##  $ airline  : chr  "United" "American Airlines" "American Airlines" "JetBlue" ...
##  $ country  : chr  "USA" "USA" "USA" "USA" ...
##  $ flight   : int  1545 441 1141 725 461 1696 507 5708 411 1545 ...
##  $ equip    : chr  "B737-8" "B777" "A321" "A321" ...
##  $ tailnum  : chr  "N14228" "N24211" "N619AA" "N804JB" ...
##  $ origin   : chr  "EWR" "MIA" "JFK" "JFK" ...
##  $ dest     : chr  "IAH" "GRU" "MIA" "BQN" ...

##Use the R function str() to understand the structure of the data frame.##

head(df.flights,4)
##   year month day dep_hr dep_min dep_delay carrier           airline country
## 1 2022     9  18      2      38         0      UA            United     USA
## 2 2022     2  14     12      25        13      AA American Airlines     USA
## 3 2022     1   7     13      56         9      AA American Airlines     USA
## 4 2022     6   7     22      56         7      B6           JetBlue     USA
##   flight  equip tailnum origin dest
## 1   1545 B737-8  N14228    EWR  IAH
## 2    441   B777  N24211    MIA  GRU
## 3   1141   A321  N619AA    JFK  MIA
## 4    725   A321  N804JB    JFK  BQN

##Use the R function str() to understand the structure of the data frame.##

tail(df.flights,5)
##    year month day dep_hr dep_min dep_delay carrier        airline country
## 14 2023     2  30     24       2         0      DL Delta Airlines     USA
## 15 2023     6   2     11       2        68      UA         United     USA
## 16 2023     6   5      5      50       850      B6        JetBlue     USA
## 17 2023    11  24     20      23        20      NK Spirit Airways     USA
## 18 2024     7  16      5      31        18      LH      Lufthansa Germany
##    flight    equip tailnum origin dest
## 14    461   B757-2  N668DN    LGA  ATL
## 15   1696 B737-MAX  N39463    EWR  ORD
## 16    507     A321  N516JB    EWR  FLL
## 17   5708     A321  N829AS    BOS  PBI
## 18    411   B747-4  N593JB    CLT  MUC

##Display only the carrier, flight, origin, and destination columns from the dataframe.##

df.flights [c(7, 10, 13, 14)]
##    carrier flight origin dest
## 1       UA   1545    EWR  IAH
## 2       AA    441    MIA  GRU
## 3       AA   1141    JFK  MIA
## 4       B6    725    JFK  BQN
## 5       DL    461    LGA  ATL
## 6       UA   1696    EWR  ORD
## 7       B6    507    EWR  FLL
## 8       NK   5708    BOS  PBI
## 9       LH    411    CLT  MUC
## 10      UA   1545    EWR  IAH
## 11      AA    441    MIA  GRU
## 12      AA   1141    JFK  MIA
## 13      B6    725    JFK  BQN
## 14      DL    461    LGA  ATL
## 15      UA   1696    EWR  ORD
## 16      B6    507    EWR  FLL
## 17      NK   5708    BOS  PBI
## 18      LH    411    CLT  MUC

##Calculate the average (mean) departure delay (column named dep_delay) and display the result in R using the cat() function. Hint: Look up functions in Help in R Studio or online.##

mean = mean(df.flights [,"dep_delay"])
cat("average departure delay =" , round(mean, 1))
## average departure delay = 69.7

##Add a new column ‘tod’ to the data frame with a value of “am” or “pm” depending whether the departure time was AM (before 12 noon) or PM (on or after 12 noon). The time in the data file is in 24-hour format. Hint: Look up how to use the ifelse function. Use the dep_hr column. Print the dataframe to ensure the new column is there and is correct, but only display the carrier, flight, dep_hr, and the new tod columns.##

df.flights$tod = ifelse(df.flights$dep_hr <12, "am", "pm")
head(df.flights[c("dep_hr", "tod", "carrier", "flight")])
##   dep_hr tod carrier flight
## 1      2  am      UA   1545
## 2     12  pm      AA    441
## 3     13  pm      AA   1141
## 4     22  pm      B6    725
## 5     24  pm      DL    461
## 6     11  am      UA   1696

##For each flight, display the carrier, flight number, and the actual departure time (scheduled departure plus departure delay) for flights that were delayed. Display the time in the format hh:mm in 24-hour format, e.g., display 23:20 rather than 11:20 or 11:20PM.##

df.flights$actualtime <- (df.flights$dep_hr * 60 + df.flights$dep_min + df.flights$dep_delay)
df.flights$actualtime <- df.flights$actualtime %% 1440

df.flights$actualtime <- sprintf(
  "%02d:%02d",
  df.flights$actualtime %/% 60,
  df.flights$actualtime %% 60)

df.flights[df.flights$dep_delay > 0, c("carrier", "equip", "actualtime")]
##    carrier    equip actualtime
## 2       AA     B777      12:38
## 3       AA     A321      14:05
## 4       B6     A321      23:03
## 5       DL   B757-2      00:16
## 6       UA B737-MAX      11:51
## 7       B6     A321      06:03
## 8       NK     A321      21:13
## 9       LH   B747-4      06:01
## 10      UA   B737-8      02:58
## 11      AA     B777      13:56
## 13      B6     A321      22:58
## 15      UA B737-MAX      12:10
## 16      B6     A321      20:00
## 17      NK     A321      20:43
## 18      LH   B747-4      05:49