library(tidyverse)
flights <- nycflights13::flightsClasswork 6
Data Transformation with R - filter(), arrange(), and distinct()
Data
Analytics
On this page
Complete the questions in this Classwork and submit your responses on the corresponding Brightspace page.
Fill in every ____ below. In the code, a blank may stand for a function, variable, operator, or value. Copy each completed code example into your R script and run it to check your answer.
Consider the nycflights13::flights data.frame.
For detailed descriptions of the variables in this data frame, load its help documentation using the following command:
# Documentation for the `flights` data.frame from the `nycflights13` package
?flights # displays the description of variables in the `flights` data.frameQuestion 1
The delay variables are measured in minutes. Complete each filter() expression. Pay attention to whether a boundary value should be included.
(a) Find all flights that had an arrival delay of two or more hours.
flights |>
____(arr_delay ____)Two hours is 120 minutes. >= includes flights delayed by exactly 120 minutes as well as longer delays.
late_arrivals <- flights |>
filter(arr_delay >= 120)
nrow(late_arrivals)[1] 10200
There are 10,200 such flights.
(b) Find all flights that flew to Houston (IAH or HOU).
flights |>
filter(dest == "IAH" ____ dest == ____)The | operator means or, so a flight qualifies if its destination is either Houston airport.
houston_flights <- flights |>
filter(dest == "IAH" | dest == "HOU")
nrow(houston_flights)[1] 9313
There are 9,313 flights to IAH or HOU.
(c) Find all flights that departed in summer (July, August, and September).
flights |>
filter(month == 7 ____ month == 8 ____ month == 9)Join the three month comparisons with | so that any one of the months qualifies.
summer_flights <- flights |>
filter(month == 7 | month == 8 | month == 9)
nrow(summer_flights)[1] 86326
There are 86,326 flights in the three specified months.
(d) Find all flights that arrived more than two hours late, but did not leave late. An on-time or early departure has a departure delay of zero or less.
flights |>
filter(arr_delay ____ 120 ____ dep_delay ____ 0)Use > because “more than” excludes exactly 120 minutes. Use & to require both conditions; <= 0 includes on-time and early departures.
late_arrival_on_time_departure <- flights |>
filter(arr_delay > 120 & dep_delay <= 0)
nrow(late_arrival_on_time_departure)[1] 29
There are 29 flights meeting both conditions.
Question 2
- How many flights have a missing
dep_time?
Complete the code to keep flights with a missing departure time, then count their rows.
flights |>
filter(____(dep_time)) |>
____()Number of flights with missing dep_time: ____
is.na(dep_time) identifies missing departure times, and nrow() counts the matching flights.
flights |>
filter(is.na(dep_time)) |>
nrow()[1] 8255
The count is 8,255.
Question 3
- Sort flights to find the most delayed flights by departure delay (
dep_delay), with the largest delay first.
head(data, n) shows the first n rows of a data frame in their current order. For example, this data frame has four rows, and head(example_flights, 2) displays only the first two:
example_flights <- data.frame(
flight = c("A", "B", "C", "D"),
dep_delay = c(10, 30, 0, 60)
)
head(example_flights, 2) flight dep_delay
1 A 10
2 B 30
With no second argument, head(data) shows the first six rows. In the exercise below, head(5) shows the first five rows from the preceding sorting step.
Complete the code to sort in descending order, then inspect the first few rows.
flights |>
____(____(dep_delay)) |>
head(5)arrange() sorts the rows, and desc() puts the largest departure delays first. head(5) then shows the first five flights in that order.
flights |>
arrange(desc(dep_delay)) |>
head(5)# A tibble: 5 × 19
year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
<int> <int> <int> <int> <int> <dbl> <int> <int>
1 2013 1 9 641 900 1301 1242 1530
2 2013 6 15 1432 1935 1137 1607 2120
3 2013 1 10 1121 1635 1126 1239 1810
4 2013 9 20 1139 1845 1014 1457 2210
5 2013 7 22 845 1600 1005 1044 1815
# ℹ 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
# tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
# hour <dbl>, minute <dbl>, time_hour <dttm>
The largest departure delay is 1,301 minutes, on flight HA 51 from JFK to HNL on January 9, 2013.
Question 4
- Was there a flight on every day of 2013?
Count unique calendar dates. A date needs its year, month, and day; the day-of-month alone repeats across months. Compare the count with the number of days in 2013.
flight_dates <- flights |>
____(year, ____, ____)
number_of_days <- ____(flight_dates)
number_of_days
number_of_days == ____Number of distinct dates: ____
Was there a flight on every day of 2013? ____
distinct(year, month, day) keeps one row for each calendar date. Compare the number of dates with 365, the number of days in 2013.
flight_dates <- flights |>
distinct(year, month, day)
number_of_days <- nrow(flight_dates)
number_of_days[1] 365
number_of_days == 365[1] TRUE
There are 365 distinct dates, and the comparison returns TRUE. The data contain a flight record for every day of 2013.