Prof. Eric A. Suess
So how should you complete your homework for this class?
- First thing to do is type all of your information about the problems you do in the text part of your R Notebook.
- Second thing to do is type all of your R code into R chunks that can be run.
- If you load the tidyverse in an R Notebook chunk, be sure to include the “message = FALSE” in the {r}, so {r message = FALSE}.
- Last thing is to spell check your R Notebook. Edit > Check Spelling… or hit the F7 key.
Homework 3:
Read: Chapter 5
Do 5.2.4 Exercises 1, 2,
Do 5.3.1 Exericise 2
library(tidyverse)
5.2.4
1.
This problem looks at the nycflights13 data set.
library(nycflights13)
flights
These questions relate to finding the flights that meet the conditions specified.
Note that dep_delay is in minutes.
- Had an arrival delay of two or more hours. So more than 2*60 minutes.
flights %>% filter(dep_delay >= 120)
- Flew to Houston (IAH or HOU)
flights %>% filter(dest == "IAH" | dest == "HOU")
or
flights %>% filter(dest %in% c("IAH", "HOU"))
- Were operated by United, American, or Delta
Lets check the airlines dataframe for the codes for these airlines.
airlines
flights %>% filter(carrier %in% c("DL", "AA", "UA"))
- Departed in summer (July, August, and September)
flights %>% filter(month %in% c(7,8,9))
- Arrived more than two hours late, but didn’t leave late
flights %>% filter(dep_delay <= 0 & arr_delay > 2*60)
- Were delayed by at least an hour, but made up over 30 minutes in flight
flights %>% filter(dep_delay >= 60 & dep_delay - arr_delay > 30 )
- Departed between midnight and 6am (inclusive)
Note that midnight is 2400, not 0.
flights %>% filter(dep_time <= 600 | dep_time == 2400 )
2.
The between() function can be used like %in%.
The between() can be used with the months to filter the rows from July, August, September.
flights %>% filter(between(month, 7, 9))
5.3.1
2.
Sort flights to find the most delayed flights. Find the flights that left earliest.
The five most delayed flights.
flights %>% arrange(desc(dep_delay)) %>%
head(5)
The five flights that left the earliest.
flights %>% arrange(dep_delay) %>%
head(5)
LS0tCnRpdGxlOiAnU3RhdC4gNDUwIFNlY3Rpb24gMSBvciAyOiBIb21ld29yayAzJwpvdXRwdXQ6CiAgd29yZF9kb2N1bWVudDogZGVmYXVsdAogIGh0bWxfbm90ZWJvb2s6IGRlZmF1bHQKIC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