Prof. Eric A. Suess

So how should you complete your homework for this class?

Homework 4:

 Read: Chapter 5
 Do 5.4.1 Exercise 4
 Do 5.5.2 Exericise 1, 4
 Do 5.6.7 Exercise 1
library(tidyverse)

5.4.1

4.

Yes. The contains() helper function picks out all of the variables in the dataset that contains the word TIME. The function is also not case sensitive.

library(nycflights13)
flights
flights %>% select(contains("TIME"))

The select() helpers are not case sensitive, when R is case sensitive.

To change the default. Don’t know why it does not show the columns like above.

flights %>% select(contains("TIME", ignore.case = FALSE))

5.5.2

1.

Minutes since midnight.

flights 

Covert dep_time and sechedule_dep_time to minutes since midnight.

dep_time %/% 100 * 60 This give the minutes since midnight.

dep_time %% 100 This gives the reminder in minutes.

flights %>% mutate(dep_time_mins = ( ( (dep_time %/% 100) * 60 ) + (dep_time %% 100)),
                   sched_dep_time_mins = ( ( (sched_dep_time %/% 100) * 60 ) + (sched_dep_time %% 100))  )

4.

Ten most delayed flights. There are no ties in these 10.

flights %>% arrange(desc(dep_delay)) %>%
  head(10)

5.6.7

1.

Brainstorm at least 5 different ways to assess the typical delay characteristics of a group of flights.

  1. median and mean of dep_delay time in minutes.
  2. sd of dep_delay time in minites
  3. median and mean of arr_delay time in minutes.
  4. sd of dep_delay time in minutes
  5. is the distribution of arr_delay symmetric or skewed? Same questions for dep_delay?

Which is more important: arrival delay or departure delay?

Arrival delay is more important.

What proportion of flights are on time or arrive early? Approximtely 60% of all flights are on time.

flights %>% summarize(flt_ontime = mean(arr_delay <= 0, na.rm = TRUE) )

Which arrier/airline has the best ontime rate?

flights %>% group_by(carrier) %>%
  summarize(flt_ontime = mean(arr_delay <= 0, na.rm = TRUE) ) %>%
  arrange(flt_ontime)

What proportion of flight are 10 mins or more late?

flights %>% summarize(flt_late10 = mean(arr_delay >= 10, na.rm = TRUE) )
flights %>% group_by(carrier) %>%
  summarize(flt_late10 = mean(arr_delay >= 10, na.rm = TRUE) ) %>%
    arrange(flt_late10)

What proportion of flight are 30 mins or more late?

flights %>% summarize(flt_late30 = mean(arr_delay >= 30, na.rm = TRUE) )
flights %>% group_by(carrier) %>%
  summarize(flt_late30 = mean(arr_delay >= 30, na.rm = TRUE) ) %>%
  arrange(flt_late30)
LS0tCnRpdGxlOiAnU3RhdC4gNDUwIFNlY3Rpb24gMSBvciAyOiBIb21ld29yayA0JwpvdXRwdXQ6CiAgd29yZF9kb2N1bWVudDogZGVmYXVsdAogIHBkZl9kb2N1bWVudDogZGVmYXVsdAogIGh0bWxfbm90ZWJvb2s6IGRlZmF1bHQKICBodG1sX2RvY3VtZW50OgogICAgZGZfcHJpbnQ6IHBhZ2VkCi0tLQoKKipQcm9mLiBFcmljIEEuIFN1ZXNzKioKClNvIGhvdyBzaG91bGQgeW91IGNvbXBsZXRlIHlvdXIgaG9tZXdvcmsgZm9yIHRoaXMgY2xhc3M/CgotIEZpcnN0IHRoaW5nIHRvIGRvIGlzIHR5cGUgYWxsIG9mIHlvdXIgaW5mb3JtYXRpb24gYWJvdXQgdGhlIHByb2JsZW1zIHlvdSBkbyBpbiB0aGUgdGV4dCBwYXJ0IG9mIHlvdXIgUiBOb3RlYm9vay4KLSBTZWNvbmQgdGhpbmcgdG8gZG8gaXMgdHlwZSBhbGwgb2YgeW91ciBSIGNvZGUgaW50byBSIGNodW5rcyB0aGF0IGNhbiBiZSBydW4uCi0gSWYgeW91IGxvYWQgdGhlIHRpZHl2ZXJzZSBpbiBhbiBSIE5vdGVib29rIGNodW5rLCBiZSBzdXJlIHRvIGluY2x1ZGUgdGhlICJtZXNzYWdlID0gRkFMU0UiIGluIHRoZSB7cn0sIHNvIHtyIG1lc3NhZ2UgPSBGQUxTRX0uCi0gTGFzdCB0aGluZyBpcyB0byBzcGVsbCBjaGVjayB5b3VyIFIgTm90ZWJvb2suICBFZGl0ID4gQ2hlY2sgU3BlbGxpbmcuLi4gb3IgaGl0IHRoZSBGNyBrZXkuCgpIb21ld29yayA0OgoKICAgICBSZWFkOiBDaGFwdGVyIDUKICAgICBEbyA1LjQuMSBFeGVyY2l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