dplyr 1: Verbs
Click the “code” button above to copy the source code into your RStudio editor. Paste into a fresh .qmd file on your computer (make sure you are in your activities folder, or wherever you save your activities, and not one of your homework repos!)
Wrangling the nycflights23 data
Practice with filter
- Find all flights that had an arrival delay of two or more hours.
- Find all flights to MSP
- Find all flights that arrived more than two hours late, but left less than one hour late
- Bonus: Find all flights that were delayed by at least an hour, but made up over 30 minutes in flight
Practice with arrange
Use arrange to answer the following questions:
- Which flights traveled the farthest?
- Which traveled the shortest?
- Which flights lasted the longest?
- Which lasted the shortest?
Practice with mutate
Create a new column in flights giving the average speed of the flight while it was in the air. What are the units of this variable? Make the variable in terms of miles per hour.
Practice with case_when
Suppose that you don’t think the FAA gives enough information in their definition of a delayed flight, so you come up with the following delay categories:
dep_delay <= 0-> nonedep_delaybetween 1 and 15 minutes -> minimaldep_delaybetween 16 and 30 minutes -> delayeddep_delaybetween 31 and 60 minutes -> majordep_delayover 60 minutes -> extreme
Use mutate() and case_when() to create a delay_category variable in the flights data frame.
Practice with pipes
Chain the last two parts together, so that the resulting dataset contains both avg_speed and delay_category. Pipe this new dataset into ggplot() to answer the question “is there a relationship between average speed and how late a flight is delayed?” You should use the delay_category variable you created to answer this question.
If you have time, create a new graph which only contains flights to MSP. Does your conclusion change?
Adapted from Adam Loy and Amanda Luby’s materials