DS 2: Seeking Help + Data Structures

Week 2 · Tuesday, Oct 6 · 9:50-10:55 am · Self-guided

Dr. Marcela Alfaro Córdoba

Goal for this morning

Work through two lessons on the Intro to R site, on your own:

By 10:55 you should have two proof-of-completion PDFs downloaded.

Important

This is the first graded DS: 3 points. 2 for the printout, 1 for Thursday’s discussion check.

Ground rules

Allowed

  • The R help pages: ?, ??, apropos()
  • The lesson page itself
  • Thursday’s slides
  • Your own notes and scripts
  • Asking me

Not allowed

  • ChatGPT, Claude, Copilot, Gemini, any chatbot
  • Positron Assistant or inline suggestions
  • Copying a classmate’s code

Talking through an idea with a neighbor is fine and encouraged. Reading their screen and typing what’s on it is not.

Before every exercise: predict

Write down what you expect, then run it.

The exercises are seeded, so your numbers differ from your neighbor’s. There’s no answer to borrow: you have to reason about your version.

Note

This is not busywork. Thursday’s discussion check asks you to predict the output of a line from your own solution without running it. If you predicted while solving, you already practiced.

How to work

  1. Open the lesson page. Read the section above the exercises first.
  2. Keep Positron open next to the browser. The in-browser boxes run R, but the help pages are better in Positron’s Help pane.
  3. Click Run in the exercise box to see your seeded values.
  4. Predict. Write it in a comment. Then write your code.
  5. Check. If it fails, read the message before changing anything.

Tip

Keep a scratch script week02/ds02_notes.R for the things you tried. It’s where your discussion-check answers come from.

Lesson 3: Seeking Help

What you’re practicing

Exercise The skill
1 — Predict the type Read ?c, then reason about coercion before running
2 — sep or collapse? Find the answer to a specific question inside a help page
3 — Find the right reader Find a function you’ve never seen, using ??

Exercise 3 is the hard one and the most realistic. That’s the whole job: “there must be a function for this” → find it → read its arguments → use it.

Tips for Lesson 3

Exercise 1. Your four c() calls are different from everyone’s. Work down the hierarchy:

logical → integer → double → complex → character. The most flexible type present wins. 2L is integer, 2 is double, "2" is character.

Exercise 2. In ?paste, read the Arguments entries for sep and collapse, then run the Examples. Ask yourself: does the result have the same length as the input, or length 1?

Exercise 3. Start with ??"read table" or ?read.table. In Arguments, look for two things: how to say “columns are separated by tabs”, and how to say “decimals are written with a comma”. Then use str() on the result: if you see chr where you expect num, it didn’t work.

If you get stuck on Lesson 3

Run the routine on yourself:

  1. Read the whole error message
  2. Re-read the line it points to
  3. Open the help page of the function in the message
  4. Try the smallest version that shows the problem
  5. Search the exact message text
  6. Ask me, with your small example

If you spend more than 10 minutes on one exercise, raise your hand. Being stuck is useful; being stuck for 40 minutes is not.

Lesson 4: Data Structures

What you’re practicing

Exercise The skill
1 — Clean a messy column str(), find the bad value, convert, verify
2 — Scale a sequence Sequences, and using variables instead of typed numbers
3 — What type are the names? typeof() on a vector vs. on its names()
4 — Number the months Built-in constants, names(x) <-, lookup by name
5 — Vector or data frame? [ ] vs [[ ]] vs $
6 — Length of a matrix A matrix is a vector with dimensions
7 — Fill by row Finding byrow in ?matrix
8 — A list of what you know Building a named list
9 — Which command made this? Reading code backwards from its output

Tips for Lesson 4, part 1

Exercise 1. Order matters: find the row first, fix the value second, convert third. If avg_height is NA, you converted before fixing. Print plants2$height_cm to see which entry isn’t a number.

Exercise 2. from:to and seq(from, to) both work. Use the variable names given, not the values you saw printed — the check runs with different numbers.

Exercise 3. Two separate calls:

typeof(bus_fares)          # the fares themselves
typeof(names(bus_fares))   # their labels

Names are always the same type, whatever the vector holds. Why?

Tips for Lesson 4, part 2

Exercise 4. Type month.abb in the console first, then LETTERS. These come with R; nothing to load.

Exercise 5. The rule: one bracket keeps the box, two brackets take the contents out. plants[1] is still a data frame; plants[[1]] is the column. Check each with class(), one at a time.

Exercise 6. Predict before running. A matrix is one long vector with a dim attribute, so length() counts elements, not rows or columns.

Exercise 9. Don’t run the four candidates first. Work out which values must be in the vector, in which order, for column-by-column and for byrow = TRUE. Then check. Two of them may both be right.

Halfway check: 10:25

By now you should have finished Lesson 3 and be somewhere in Lesson 4.

Behind? That’s fine. Priorities:

  1. Finish Lesson 3 completely (all 3)
  2. In Lesson 4, do 1, 4, 5 and 9 — the ones we discuss on Thursday
  3. Come back to the rest tonight

Done early? Don’t leave. Walk over to someone who’s stuck and ask them what they’ve tried, don’t tell them the answer. Explaining is how you get the extra point on Thursday.

Finishing up

Get your PDFs

At the bottom of each lesson page:

  1. Make sure your name is filled in on the site
  2. Attempt every exercise (an honest wrong attempt counts)
  3. Click Download my proof-of-completion PDF

The PDF shows your name, which exercises you passed, how many attempts, and your final code for each one.

Warning

Two lessons this week means two PDFs. 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.

What to submit and bring

Upload to Canvas by Wednesday 11:59 pm

  • Both PDFs

Bring to Thursday’s lecture

  • Both PDFs, printed on paper
  • A pencil

On Thursday we go through it on paper, and you’ll be asked to explain a line of your own solution or predict what it prints.

Late: up to 3 days, 5% off per 24 hours. The lowest DS grade is dropped.

Before you go: checklist

See you at 1:30 pm for Exploring Data Frames, where we load real data. Bring your laptop charged, and make sure that CSV is there.