library(tidyverse)
library(nycflights23)
q <- 0Lab Quiz 02 Info
Our second lab quiz is scheduled for Friday of Week 5. The first half of class will be spent taking the quiz, and the second half will cover new content.
Guidelines
This is a closed note, closed internet resources, closed other people lab quiz. I want to see what’s in your brain! You may use the cheat sheets provided by me, but otherwise you may not use any resources.
The lab quizzes are not written to be tricky or very difficult. If you’ve been completing the in-class activities and the homework, and putting the time and effort in to understand them, you should do well on the lab quizzes.
Format
The reason I give lab quizzes is because your life will be easier if you know how to do basic tasks in R “on the fly”. I also want to see what you know and not just what you can do with access to your resources. However, I also know this is not how data science works in the real world, and so there are two portions to the lab quiz.
- The required in-class portion (“first pass”)
- Pencil-and-paper questions
- Cheat sheets provided by me but no other resources
- Designed to assess what you know “on the fly” without access to resources
- An optional R-based “second pass” at the questions
- .qmd format similar to homework
- Allowed to use materials from class (slides, activities, notes, homework, etc.) but no out-of-class resources (google, textbooks, ChatGPT or other LLMs, StackOverflow, etc.)
- Designed to assess your coding skills and whether you can figure things out with access to class resources and R help pages
Your score on the quiz will be the average of the two scores, unless you do not submit the revision, in which case your score will only be in your in-class score. If you earn 100% on the in-class portion, your overall score will be 100% and I will not grade your revision.
Skills
Data Wrangling
- Know how the following verbs act on a data set:
filter,distinct,slice,slice_min,slice_max,mutate,select,arrange,relocate,*_join(all flavors discussed in class),bind_rows,bind_cols,pivot_wider, andpivot_longer,separate,unite,group_by,ungroup,summarize,count,rowwise- Syntax for using
- Describe what the output would look like
- Given a data set and goal, identify and utilize the appropriate verb to create the data set of interest
- Given a dataset, describe if it is tidy. If it is not, describe what a tidy format would look like
Dates/Times
- Use {lubridate} functions to parse dates into ISO8601 format
-
mdyymddmyetc.
-
- Use {lubridate} functions to to extract elements of a date/time
-
yearmonthdayweekwdayetc.
-
Factors
- Create a factor to represent a categorical variable
- Given a factor, modify the levels as needed for the analysis by recoding, reordering, or combining levels.
-
fct_reorder,fct_reorder2,fct_infreq,fct_rev,fct_recode
-
Grading
For the in-class portion, each question is worth 1 point, graded as “successful” (almost entirely correct), “not successful” (missing a key component), or “half credit” (mostly correct).
For the out-of-class revision, each question is worth 1 point, graded as “successful” (correct), “not successful” (missing a key component), or “half credit” (mostly correct). Since you will be able to run the code, you will only receive full credit for fully correct solutions.
Revision
The in-class portion will be a pencil-and-paper quiz. After class, I will make everyone a repository on github with a .qmd version of the quiz. (The questions will remain essentially the same). The revision is totally optional. You will work on the repository just like homework and submit via gradescope if you wish to turn it in.
- The in-class portion is worth 10 points
- The out-of-class “second pass” is worth 10 points
- If you don’t submit the revision on gradescope, your quiz will be out of 10 points and if you do submit the revision your quiz will be out of 20 points.
The revision is due before class starts on Monday. You may use the cheat sheets, materials from our class (slides, activities, notes, your homework, etc.), and the built-in help pages within RStudio. You may not use textbooks, LLMs, search engines, stack overflow, etc. to complete the revision. If your submission contains code that did not appear in our course materials, you must include an explanation for where it came from (e.g. in what context you learned that code). Otherwise, you will not receive credit for that problem and may be reported to the Academic Standing Committee.
Practice Questions
Data
The nycflights23 package contains information about all flights that departed from NYC (e.g. EWR, JFK and LGA) in 2023. The main data is in the flights data frame, but there are additional data sets which may help understand what causes delays, specifically:
-
weather: hourly meteorological data for each airport -
planes: construction information about each plane -
airports: airport names and locations -
airlines: translation between two letter carrier codes and names
Questions
Know how the following verbs act on a data set
Carefully and clearly describe what each set of commands does
cleaned <- flights |>
filter(origin == "JFK" & dest == "MSP")Explain why na.rm = TRUE is included in the code chunk from part (b)
Given a data set and goal, identify and utilize the appropriate verb to create the data set of interest
Use the plot below to answer the questions.

- To create this plot, I had to do a few things. First, I combined the
flightsdataset (which contains information about arrival delay) with theairportsdataset (which contains the full name of the airports, along with their time zones). Did I use aleft_join,full_join,bind_rows, orbind_colsto combine them? Explain how you can tell.
glimpse(flights)Rows: 435,352
Columns: 19
$ year <int> 2023, 2023, 2023, 2023, 2023, 2023, 2023, 2023, 2023, 2…
$ month <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
$ day <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
$ dep_time <int> 1, 18, 31, 33, 36, 503, 520, 524, 537, 547, 549, 551, 5…
$ sched_dep_time <int> 2038, 2300, 2344, 2140, 2048, 500, 510, 530, 520, 545, …
$ dep_delay <dbl> 203, 78, 47, 173, 228, 3, 10, -6, 17, 2, -10, -9, -7, -…
$ arr_time <int> 328, 228, 500, 238, 223, 808, 948, 645, 926, 845, 905, …
$ sched_arr_time <int> 3, 135, 426, 2352, 2252, 815, 949, 710, 818, 852, 901, …
$ arr_delay <dbl> 205, 53, 34, 166, 211, -7, -1, -25, 68, -7, 4, -13, -14…
$ carrier <chr> "UA", "DL", "B6", "B6", "UA", "AA", "B6", "AA", "UA", "…
$ flight <int> 628, 393, 371, 1053, 219, 499, 996, 981, 206, 225, 800,…
$ tailnum <chr> "N25201", "N830DN", "N807JB", "N265JB", "N17730", "N925…
$ origin <chr> "EWR", "JFK", "JFK", "JFK", "EWR", "EWR", "JFK", "EWR",…
$ dest <chr> "SMF", "ATL", "BQN", "CHS", "DTW", "MIA", "BQN", "ORD",…
$ air_time <dbl> 367, 108, 190, 108, 80, 154, 192, 119, 258, 157, 164, 1…
$ distance <dbl> 2500, 760, 1576, 636, 488, 1085, 1576, 719, 1400, 1065,…
$ hour <dbl> 20, 23, 23, 21, 20, 5, 5, 5, 5, 5, 5, 6, 5, 6, 6, 6, 6,…
$ minute <dbl> 38, 0, 44, 40, 48, 0, 10, 30, 20, 45, 59, 0, 59, 0, 0, …
$ time_hour <dttm> 2023-01-01 20:00:00, 2023-01-01 23:00:00, 2023-01-01 2…
glimpse(airports)Rows: 1,251
Columns: 8
$ faa <chr> "AAF", "AAP", "ABE", "ABI", "ABL", "ABQ", "ABR", "ABY", "ACK", "…
$ name <chr> "Apalachicola Regional Airport", "Andrau Airpark", "Lehigh Valle…
$ lat <dbl> 29.72750, 29.72250, 40.65210, 32.41130, 67.10630, 35.04020, 45.4…
$ lon <dbl> -85.02750, -95.58830, -75.44080, -99.68190, -157.85699, -106.609…
$ alt <dbl> 20, 79, 393, 1791, 334, 5355, 1302, 197, 47, 516, 221, 75, 18, 7…
$ tz <dbl> -5, -6, -5, -6, -9, -7, -6, -5, -5, -6, -8, -5, -10, -6, -9, -6,…
$ dst <chr> "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",…
$ tzone <chr> "America/New_York", "America/Chicago", "America/New_York", "Amer…
- Next, I
grouped by airport name and found the median arrival delay withsummarize. I then created a new variable calledcolorbyusingmutatethat I used to color the points. How do you think I computed this variable? Be as specific as possible.
Given a dataset, describe if it is tidy. If it is not, describe what a tidy format would look like
The dataset below contains the minimum and maximum wind speed recorded at JFK for each day in the dataset. Is this tidy? If not, how would we make it tidy? (You should provide R code AND sketch the tidy dataset)
# A tibble: 728 × 5
year month day name value
<int> <int> <int> <chr> <dbl>
1 2023 1 1 min_wind_speed 0
2 2023 1 1 max_wind_speed 15.0
3 2023 1 2 min_wind_speed 4.60
4 2023 1 2 max_wind_speed 11.5
5 2023 1 3 min_wind_speed 0
6 2023 1 3 max_wind_speed 10.4
7 2023 1 4 min_wind_speed 0
8 2023 1 4 max_wind_speed 9.21
9 2023 1 5 min_wind_speed 0
10 2023 1 5 max_wind_speed 10.4
# ℹ 718 more rows
Use {lubridate} functions to parse dates into ISO8601 format
- How would you parse
2/8/2023to ISO format? - How would you parse
8 Feb 2023to ISO format?
Use {lubridate} functions to to extract elements of a date/time
Use mutate to create a new column in the flights dataset that contains the day of the week of the flight (“Monday”, “Tuesday”, etc.).
Create a factor to represent a categorical variable
Use mutate to create a new column in the flights dataset that contains the season of the flight according to the following:
- December - February: Winter
- March - May: Spring
- June - August: Summer
- September - November: Fall
Given a factor, modify the levels as needed for the analysis by recoding, reordering, or combining levels
Here is the code that I used to create the ggplot. Edit this code so that the airports are listed in order of shortest arrival delay at the top to longest arrival delay at the bottom
plot_data = read_csv("https://stat220-s25.github.io/data/lq2_plot_data.csv")
plot_data %>%
ggplot(
aes(x = med_arr_delay, y = name, col = colorby)
) +
geom_point() +
theme(legend.position = "bottom") +
labs(
title = "Median Arrival Delay for flights from NYC airports in 2023",
subtitle = "Among West Coast time zone airports"
)