Homework 2
CDS-101 (Spring 2017)
Name: Muaad Kholi
Question 1:
Vector of numbers between 1 and 100 that are indivisible by 2, 3 and 7: (Spring 2017)
For this task I created three variables:
Variable v1 that contained a vector of all the numbers between 1 and 100.
The second variable "indx" contained all the numbers between 1 and 100 that are divisible by 2, or divisible by 3, or divisible by 7.
The third variable, "others", had v1 without the indx variable, leaving us with all the numbers between 1 and 100 that are indivisible by 2, 3 or 7.
Code looks as follows:
v1 <- 1:100
indx <- v1 %% 3 & v1 %% 2 & v1 %% 7
(others <- v1[!!indx])
The code below cretes variable "v1" which is a vector from 1 to 100. Then it creates variable "indx" and assigns it the values of MOD(v1, 3) AND MOD(v1, 2) AND MODA(v1, 7) The code then creates variable "others and assigns it all the values of variable v1, EXCLUDING the values that are in the indx variable.
Question 2:
Find flights that:
Flew to Houston (IAH or HOU)
Were operated by United, American, or Delta.
Were delayed by at least an hour, but made up over 30 minutes in flight.
Departed between midnight and 6am (inclusive).
Answer to Question 2.1.
I'm creating a variable "Houston_Flights" and assigning the subset of data to it.
The next line, I printed the variable to confirm the code.
Answer to Question 2.2.
I'm creating a variable "UAD" and assigning the subset of data to it that has flights by united, American, and Delta.
The code below creates variable UAD, and assigns it a a filtered set of the dataset with all the records that have "AA" OR "UA" OR "DL" in teh carrier column.
Answer to Question 2.3.
I'm creating a variable "Del_but_made" and assigning the subset of data to it of flights that were delayed by at least an hour but made over 30 minutes in flight.
The code below creates variable "Del_but_made" and assigns it a filtered version of the dataset that contains flights with a dep_delay value bigger than 59 (delayed by at least 60 minutes) AND arr_delay value larger than 30 minutes.
Answer to Question 2.4
To answer this question, I'm creating a variable " mid_6" with flights that departed between midnight and 6am (inclusive).
The code below creates variable "mid_6" and assigns it a subset of the data that is filtered with only flights that have a departure time of 600 or below (flights departed between midnight and 600 in the morning.
Question 3:
Currently dep_time and sched_dep_time are convenient to look at, but hard to compute with because they’re not really continuous numbers. Convert them to the more convenient representation of number of minutes since midnight.
The code below create variable "dep_time_in_minutes" and assigns it a "transmuted" flights dataset with a column "dep_time" that contains the value of "hour" multiplied by 60 and added to the value of minute.
The code below creates variable "times_Parsed" which contains the dataset with two new columns; sched_hour and sched_minute.
The code below creates variable "sched_dep_time_in_minutes" and assigns it a dataset with mutated column "sched_dep_time_minutes" which contains the value of "sched_hour" multiplied by 60, and added to the value of "sched_minute" which calculate the scheudled departure time in minutes total since midnight.
Question #4:
Compare air_time with the difference arr_time − dep_time. What did you expect to see? What do you need to do to fix it? Implement the fix.
Answer to question 4:
After examining the data, 3 issues were identified with the dep_time - arr_time discrepancy when compared with airtime:
when substracting the dep_time from the arr_time, R is treatign these values as if they were number, and not time. To solve this issue, the columns must be formated in time format, or converted to minutes before they are substracted.
The departure time is clocked at the city of departure, while the arrival time is clocked at the city of arrival, which in many cases resides in different time zones. The solution for this issue is to convert both times to a standard time, such as Zulu time, based on thier respective time zones.
The third issue is that the arrival and departure time may include time on the runway, as long as the plane is moving using its own engines for the purpose of departing or embarking. Airtime is the time in the air only which only counts the time from when the wheels leave the runway and until they touch the runway again when landing. This can only be solved by using different datapoints.
Question 5:
Consider the number of canceled flights per day in the dataset.
Review the dataset and determine what would be reasonable definition of a flight cancella- tion. Filter the dataset so that only the canceled flights remain. Note that the Boolean test (is.na(dep_delay) | is.na(arr_delay)) is not the best possible definition.
Calculate the number of canceled flights per day using the filtered dataset. Is there a pattern? Is the proportion of canceled flights related to the average delay?
Answer for question 5.1:
After reviewing the data, the following citeria woul best fit cancelled flights: flights with NA for departure time OR flights with NA for arrival time OR flights with NA for airtime. Dataset filtered below.
Answer for question 5.2:
To answer this quesiton, I calculated the MEAN of departure delay time for calncelled flights, and the MEAN departure delay for all the flights in the dataset (calculations below.) The calculation indicates that calcelled flights's mean delay time was 36 minutes, while teh entire dataset had a mean of 12.6, less than half that of cancelled flights, which means that flight cancellation is related to the average dealy.
The code below creates variable "cancellations" and assigns it to a filtered dataset that includes only records with NA as thier departure time OR NA forarrival time OR NA for air time.
The code below calculates and displays the mean of the dep_delay column (excluding NA values) within the "cancellations" variable, which contains only cancelled flights.
The code below calculates and displays the mean of the dep_delay column (excluding NA values) within the entire dataset
Question #6:
What time of day should you fly if you want to avoid delays as much as possible?
Answer to question #6:
To answer this quesiton, I created a variable (by_time) thich contained the dataset grouped by scheduled departure time. Then I summarized that the data within the new variable by the mean of its departure delay value. From the results, the best time to schedule a flight is at 548, when the mean delay value is closest to 0, as low as 0.07692308.
The code below creates variable "by_time" and assigns it a table with all the unique values for "sched_dep_time" with the mean value for their respective "dep_delay".