Quiz 2 Study Guide
Weeks 3 - 6
What to expect
Quiz 2 covers weeks 3 through 6. You are allowed one page of notes. See the last section for tips on making it. It will be 4 questions on 2 pages.
Examples of questions topics include:
- put pipeline steps in order
- find bugs in code you read
- predict what a join, a pivot, or a type coercion does
I will not ask you to write code from scratch. You will read code, think about what it does, and mark up tables. So focus on what each function does, not the details of the arguments.
Week by week summary
Week 3: Wrangling a single data frame
Every dplyr verb takes a data frame and returns a data frame, so you can chain them together with a pipe. The order of the steps matters.
- Key functions:
select(),filter(),arrange()anddesc(),mutate(),count(),group_by(),summarise() =assigns a value or names an argument.==tests for equality. You need==insidefilter().- dplyr steps are connected with a pipe (
|>or%>%). ggplot2 layers are connected with+. group_by()has to come beforesummarise().- After a
select()or asummarise(), only some columns are left. Anything you need later has to still be there. - From week 2:
aes()maps variables to features of the plot,geom_*()draws them, andlabs()labels them. Ifcolordepends on a variable, it goes insideaes().
Week 4: Exploratory data analysis
Look at each variable on its own (variation), then look at how variables relate (covariation). When you find an unusual or missing value, you have to decide what to do about it.
- Key functions:
geom_histogram(),geom_density(),geom_boxplot(),coord_cartesian(),fct_reorder(),coord_flip() - There are two options for an unusual value. You can drop the whole row, or replace the value with
NA. Replacing is usually better because the rest of that row is still good data.
Week 5: Data types, classes, and import
R decides what type each column is when it reads in your data. One stray text value turns the whole column into character, and then you can’t do math on it. If your data isn’t behaving, check the types first.
- Key functions:
read_csv(),read_excel(),janitor::clean_names(),rename(),as.numeric(),as.character(),ymd(),fct_relevel(),write_rds()andread_rds() - Data types: logical, integer, double, and character. A vector holds only one type, so when you mix types, R coerces everything to the most flexible one, which is character.
NAis logical. Know whatTRUE | NAandFALSE | NAgive you.- Classes: factors, dates, data frames, and lists.
- Bad column? You can fix it when you read the data in (
na =orcol_types =), or convert it after.
Week 6: Joins, pivots, and recoding
Combine tables with a join and reshape a table with a pivot. Picture the result before you run the code.
- Key functions:
left_join(),right_join(),inner_join(),full_join(),semi_join(),anti_join(),join_by(),pivot_longer(),pivot_wider(),case_when(),fct_rev() - For each join, know which rows are kept and which get
NAs. Know whatanti_join()returns. - Know the difference between wide and long data, and why we pivot to long before plotting.
case_when()checks each condition in order, and each one gives you a new value. If you want to keep everything else as it was, add a last conditionTRUE ~ column_name.
Advice on how to prepare
- Watch the videos again Or the first time if you skipped them earlier.
- Finish or correct missing code alongs I won’t accept it for credit, but you can never get enough practice fixing your mistakes.
- Predict, then check. Before you run any “You try it” chunk, write down what you expect. Then run it and compare.
- Break it on purpose. Take a working code chunk and make one error in it. Read the error message.
- Draw the join and/or pivot. Make small tables on paper and draw what each one does to the rows and columns.
- Re-order shuffles. Copy a pipeline from the lecture, cut it into one step per line, shuffle, and put it back in order. Explain why each step goes where it does.
- Explain it to a classmate without looking. If you can’t, that’s the topic to review.
Your one page of notes
- Make it yourself. Writing it is part of studying, and a classmate’s page won’t match what you need.
- Organize it by task, not by date. For example: “when I filter…”, “when my column is the wrong type…”, “when I join two tables…”.
- Annotate code. A short pipeline template and a one-line meaning of each join help more than copied slide text.
- Put a tiny example next to each rule. For example,
anti_join(x, y)gives the rows inxwith no match iny.