Week 3 · Tuesday, Oct 13 · 9:50-10:55 am · Self-guided
Work through one lesson on the Intro to R site, on your own:
By 10:55 you should have one proof-of-completion PDF downloaded, and a script that re-runs your work in Positron.
Important
3 points, as always: 2 for the printout, 1 for the discussion check, this time on Tuesday, Oct 20, 1:30 pm.
Allowed
?, ??, apropos()Not allowed
Talking through an idea with a neighbor is fine. Reading their screen and typing what’s on it is not.
Run the first two steps on paper, then run the code.
For a loop, write down what the loop variable holds and what the result looks like after step 1 and step 2:
Note
The discussion check (Tuesday, Oct 20) will ask you to do exactly this on a loop from your own printout, without a laptop.
Open stat266a in Positron and create week03/ds03_notes.R:
gm. Do you get what you expected?Tip
The browser forgets your work if the cache clears. Your script doesn’t. And at 10:50, the restart test: Session → Restart R, source week03/ds03_notes.R, and it should run to the end.
week03/ds03_notes.RIf you spend more than 10 minutes on one exercise, raise your hand. Being stuck is useful; being stuck for 40 minutes is not.
| Exercise | The skill |
|---|---|
| 1. Is that year in the data? | if/else with any(); the one-line if |
| 2. Grow vs. fill | Pre-allocating, and computing a position in a nested loop |
| 3. Continents above or below | Looping over groups, storing results in a named vector |
| 4. Three bands per country | if / else if / else inside a loop |
| 5. Advanced: flag some countries | Two conditions with &&, collecting matches |
Exercises 3 and 4 are the core of the lesson and the ones we discuss on Tuesday, Oct 20. Exercise 5 is the hardest. Do it last.
Exercise 1. gm$year == y1 is a whole vector of TRUE/FALSE. if needs one value. Which of any() or all() answers “are there any rows from that year”?
Use y1 and y2, not the years you saw printed. The check runs with different values.
Exercise 2. Before writing code, fill in this table by hand for your n_inner:
i |
j |
value i * 10 + j |
position in filled |
|---|---|---|---|
| 1 | 1 | 11 | 1 |
| 1 | 2 | 12 | 2 |
| 2 | 1 | 21 | ? |
The ? is the whole exercise. It depends on n_inner, not on n_outer.
Exercise 3. Thursday’s “storing one result per group” slide is almost the answer, with two differences: gm$continent is a factor, so loop over levels(); and the cutoff is yours, in cutoff.
Check with length(verdict): one entry per continent, and names(verdict) should be the continents.
Exercise 4. Inside the loop, first pull out one number: that country’s 2007 life expectancy. Print it for the first country to make sure it’s one number, not a vector.
Then the bands. Re-read the boundaries: "mid" includes lo and excludes hi. Which comparison is < and which is <=?
Break it into pieces and test each one outside the loop first, on one country you pick:
Then combine the two with &&, inside the loop. Collect matches by growing: flagged <- c(flagged, ctry).
Extra, in your script: for each flagged country, plot(year, lifeExp, type = "l") using the full gapminder data. Put the plotting loop in week03/ds03_notes.R too.
By now you should be done with Exercises 1 and 2 and working on 3.
Behind? That’s fine. Priorities:
Done early? Walk over to someone who’s stuck and ask them to trace their loop for you, out loud. Don’t tell them the answer.
At the bottom of the lesson page:
Warning
Do it now, in the room. The site stores your work in your browser: a cleared cache or a different laptop and it’s gone. Your week03/ds03_notes.R is your backup.
Upload to Canvas by Wednesday 11:59 pm
Bring to Tuesday, Oct 20, 1:30 pm
Late: up to 3 days, 5% off per 24 hours. The lowest DS grade is dropped.
See you at 1:30 pm for Cleaning data in practice. Before then, download the dataset we’ll clean into your data/ folder: field_survey.csv (right-click → Save link as…).

STAT 266A · Fall 2026 · UC Santa Cruz