---
title: 'Stat. 450 Section 1 or 2: Homework 3'
output:
  word_document: default
  html_notebook: default
  pdf_document: default
  html_document:
    df_print: paged
---

**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


```{r message=FALSE}
library(tidyverse)
```

# 5.2.4

## 1. 

This problem looks at the nycflights13 data set.

```{r}
library(nycflights13)

flights
```

These questions relate to finding the flights that meet the conditions specified.  


```{r}
help(flights)
```

Note that dep_delay is in minutes.


1. Had an arrival delay of two or more hours.  So more than 2*60 minutes.

```{r}
flights %>% filter(dep_delay >= 120)
```

2. Flew to Houston (IAH or HOU)

```{r}
flights %>% filter(dest == "IAH" | dest == "HOU")
```

or

```{r}
flights %>% filter(dest %in% c("IAH", "HOU"))
```

3. Were operated by United, American, or Delta

Lets check the airlines dataframe for the codes for these airlines.

```{r}
airlines
```


```{r}
flights %>% filter(carrier %in% c("DL", "AA", "UA"))
```

4. Departed in summer (July, August, and September)

```{r}
flights %>% filter(month %in% c(7,8,9))
```

5. Arrived more than two hours late, but didn’t leave late

```{r}
flights %>% filter(dep_delay <= 0 & arr_delay > 2*60)
```

6. Were delayed by at least an hour, but made up over 30 minutes in flight

```{r}
flights %>% filter(dep_delay >= 60 & dep_delay - arr_delay > 30  )
```

7. Departed between midnight and 6am (inclusive)

Note that midnight is 2400, not 0.

```{r}
flights %>% filter(dep_time <= 600 | dep_time == 2400 )
```


\newpage

## 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.

```{r}
flights %>% filter(between(month, 7, 9))
```

\newpage

# 5.3.1

# 2. 

Sort flights to find the most delayed flights. Find the flights that left earliest.

The five most delayed flights.

```{r}
flights %>% arrange(desc(dep_delay)) %>%
  head(5)
```

The five flights that left the earliest.

```{r}
flights %>% arrange(dep_delay) %>%
  head(5)
```




