
Lecture 8
Duke University
STA 199 - Fall 2026
September 21, 2026
Can you guess the variable plotted here?


Go to wooclap.com and use the code PYDPUEI.
Any questions about how we left off last time to where AE-03 goes by the end? Or where to find the AE answers?
Where we left off:

Where the AE-03 ends:

# A tibble: 51 × 7
state state_tax_rate state_tax_rank avg_local_tax_rate max_local
<chr> <dbl> <dbl> <dbl> <dbl>
1 Alabama 4 40 5.46 11
2 Alaska 0 46 1.82 7.85
3 Arizona 5.6 28 2.92 5.3
4 Arkansas 6.5 9 2.96 6.12
5 California 7.25 1 1.74 5.25
6 Colorado 2.9 45 4.99 9.1
7 Connecticut 6.35 12 0 0
8 Delaware 0 46 0 0
9 Florida 6 17 0.98 2
10 Georgia 4 40 3.49 5
# ℹ 41 more rows
# ℹ 2 more variables: combined_tax_rate <dbl>, combined_rank <dbl>
Put the following steps in order to compare the average state sales tax rates of swing states (Arizona, Georgia, Michigan, Nevada, North Carolina, Pennsylvania, and Wisconsin) vs. non-swing states:
swing_stateswing_state with levels "Swing" and "Non-swing"
Go to wooclap.com and use the code PYDPUEI.
First, create a vector called list_of_swing_states that contains the names of the swing states.
Then, fill in the blanks to create a new variable called swing_state with levels "Swing" and "Non-swing":

Go to wooclap.com and use the code PYDPUEI.
if_else()
Go to wooclap.com and use the code PYDPUEI.
Suppose you’re tasked with the following:
Compare the average state sales tax rates of states on the Pacific Coast, states on the Atlantic Coast, and the rest of the states.
How would you approach this task?
coast with levels "Pacific", "Atlantic", and "Neither"coastFirst, create two vectors called pacific_coast and atlantic_coast that contain the respective states.
Then, fill in the blank to create a new variable called coast:

Go to wooclap.com and use the code PYDPUEI.
case_when()Compare the average state sales tax rates of states on the Pacific Coast, states on the Atlantic Coast, and the rest of the states.
Suppose you’re tasked with the following:
Compare the average state sales tax rates of states in various regions (Midwest - 12 states, Northeast - 9 states, South - 16 states, West - 13 states).
How would you approach this task?
region with levels "Midwest", "Northeast", "South", and "West".regionmutate() with case_when()Who feels like filling in the blanks lists of states in each region? Who feels like it’s simply too tedious to write out names of all states?
list_of_midwest_states <- c(___)
list_of_northeast_states <- c(___)
list_of_south_states <- c(___)
list_of_west_states <- c(___)
sales_taxes <- sales_taxes |>
mutate(
region = case_when(
state %in% list_of_midwest_states ~ "Midwest",
state %in% list_of_northeast_states ~ "Northeast",
state %in% list_of_south_states ~ "South",
state %in% list_of_west_states ~ "West"
)
)Suppose we want to answer questions like:
Is there a relationship between
- number of QS courses taken
- having scored a 4 or 5 on the AP stats exam
- motivation for taking course
- …
and performance in this course?
Each of these would require joining class performance data with an outside data source so we can have all relevant information (columns) in a single data frame.
Suppose we want to answer questions like:
Compare the average state sales tax rates of states in various regions (Midwest - 12 states, Northeast - 9 states, South - 16 states, West - 13 states).
This can also be solved with joining region information with the state-level sales tax data.
For the next few slides…
left_join()right_join()full_join()inner_join()anti_join()Compare the average state sales tax rates of states in various regions (Midwest, Northeast, South, West), where the input data are:
sales-taxes-26.csvus-regions.csvae-04-taxes-joinGo to your ae project in Positron.
If you haven’t yet done so, make sure all of your changes up to this point are committed and pushed, i.e., there’s nothing left in your source control pane.
If you haven’t yet done so, pull to get today’s application exercise file: ae-04-taxes-join.qmd.
Work through the application exercise in class, and render, commit, and push your edits by the end of class.