HW6: map()
Download and Instructions
Here’s your sixth set of koans. This set teaches map(), the tidyverse function that runs a function once for every element of a vector or list. In HW5 you wrote functions; in this homework you run them dozens or hundreds of times without copying and pasting, which is what every simulation in HW7 and HW8 is made of.
There are four koans. The first is a review of dplyr, ggplot2, and lm() on a data set you haven’t seen: the U.S. Department of Education’s College Scorecard, with the cost, graduation rate, and share of low-income students at 1,438 four-year colleges alongside what their students were earning ten years after they enrolled. The question is what makes UC Riverside, which U.S. News has repeatedly ranked first in the country for social mobility, special: you find that across the country earnings and graduation rates fall as the share of low-income students rises, and that UC Riverside, where half the students are low-income, beats the line by about $20,000. The second koan introduces map() with functions that have names: mean, length, and functions you write in HW5’s style, including one that draws a random sample of schools and fits a line to it. You meet map_dbl(), map_chr(), and map_lgl(), which hand back a vector instead of a list, and bind_rows(), which stacks a list of tibbles into one. The third koan does the same work with anonymous functions written in place with \(x), and ends with the first simulation of the course: two hundred random samples of a hundred schools, the slope of each, and a histogram of the two hundred slopes. The fourth koan is short, and introduces the four functions R has for the normal distribution: rnorm() draws random numbers, dnorm() is the height of the bell curve, pnorm() is the area to the left of a value, and qnorm() turns an area back into a value.
Getting the data
The data set is public, so there is nothing to paste from Canvas. The first koan reads a trimmed copy straight from the course website, which means you need an internet connection while you work (the other three koans re-run the first). If read_csv() fails with a message about a URL or a connection, check that you are online and try again.
Setting up
First, download koans6.zip by clicking the link in the first paragraph. Unzip it, and then rename the unzipped folder so that it has your name in it, for example koans6_lastname. Do this before you open anything. Your Downloads folder already holds the earlier koans folders and their zip files, and they all look alike. Renaming right away means you always know which folder you are editing and, when you finish, which folder to zip and upload. The original koans6.zip stays in Downloads with the unedited koans in it, so if you skip this step it is easy to upload the wrong one.
Then open the project by double-clicking koans6.Rproj inside your renamed folder (not the individual .R files). If nothing happens when you double-click koans6.Rproj, open RStudio and navigate to File > Open Project, and then find koans6.Rproj inside koans6_lastname in your Downloads folder.
You set up the keyboard shortcut for running tests in HW1. If you’re on a new computer, redo that step:
Macs: Tools > Modify keyboard shortcuts > Run a test file > Cmd Shift T > Apply
Windows: Tools > Modify keyboard shortcuts > Run a test file > Ctrl Shift T > Apply
Now open koan_01_uc_riverside.R in the R/ folder and run it top to bottom before you run any tests. The first thing it does is view(scorecard), which opens the data in a tab; once you have had a look, put a # in front of that line, because the tests re-run the file and you don’t want the tab popping open every time. Koans 2 and 4 each begin with the line source("R/koan_01_uc_riverside.R"), which re-runs the first koan, and koan 3 begins with source("R/koan_02_map_with_names.R"), which re-runs the second (and, through it, the first). So whenever you restart R, that one line brings the data back. It also means a syntax error in koan 2 stops koan 3 from running: finish koan 2 before you start koan 3.
Then work through the remaining koans in order: koan_02_map_with_names.R, koan_03_map_without_names.R, and koan_04_the_normal_distribution.R. They work the same way as HW2 through HW5: each file teaches with live examples that you should run as you read, and under each question header you’ll find a copy of the most recent example, which you edit into your answer. Run your code in the console (Ctrl/Cmd + Enter) and look at the result, save your edits (Ctrl/Cmd + S), and press Ctrl+Shift+T (Cmd+Shift+T on Mac) to run the tests.
Three things to watch for. First, as in HW5, when a question asks you to write a named function and then use it, both go under the question header: the definition first, then the line that uses it. The tests look for the function by the name the question gives it (is_high, describe, sample_slope), check its arguments, call it on inputs of their own, and then check the last line. Second, several questions ask for a vector and not a list, or a list and not a vector. map() returns a list; map_dbl(), map_chr(), and map_lgl() return vectors. If the test says your answer is the wrong kind of thing, that is the fix. Third, the questions that draw random samples or random numbers give a different answer every time you run them, so there is no number to match. The tests check that the answer has the right length, that its values are plausible, and that it is in fact random; where a function of yours is involved, they call it many times and check that its results average out where they should.
The same two cautions carry over from earlier homeworks: a question you haven’t edited yet is reported as skipped, and a syntax error in one file stops the tests for all four, with a message that points at the line.
Turning it in
Before you zip anything, run the tests one last time and read the output. Every koan must be at 100% passing, with no errors. Do not upload a submission that stops with an error, and do not upload one with failing or skipped tests. My grader runs the same tests you do, so if the tests stop on your computer they stop on mine too, and questions the tests cannot reach get no credit even if the code underneath them is right. If the tests will not run, that is the thing to fix, not something to upload and hope about. Also check that your edits are saved: the tests read the files on disk, so if the test output does not match what you see in the editor, you have unsaved changes.
When every test passes, go to your Downloads folder and find the folder you renamed, koans6_lastname. Compress that folder into a zip file (not the original koans6.zip you downloaded, and not one of the earlier homework folders). Finally, upload that zip file to Canvas for me to grade. The koans are due before class on Monday 11/9. If you get stuck, come to office hours or email to make an appointment.
Check Your Understanding
The twenty multiple choice questions below cover the four koans in this set: handing a named function to map() and getting a list or a vector back, writing the function in place with \(x), filling in a second input, repeating a random experiment and stacking the results, and the four normal distribution functions rnorm(), dnorm(), pnorm(), and qnorm(). Every question restates the code and data it needs, so you can answer each one from what’s on the screen.