Introduction to R
  • Schedule
  • Syllabus
  • Instructors
  • Practice
  • Project
  • Resources

On this page

  • Questions
    • Question 1: Getting to Know the HRS Dataset
    • Question 2: Functions
    • Question 3: Objects
    • Question 4: Common Errors
    • Question 5: Getting Help

Practice 3 Answers

PUBH 523/623

Author

Nicky Wakim

Modified

July 1, 2026

Questions

Question 1: Getting to Know the HRS Dataset

In Lesson 11, we were introduced to the synthetic HRS (Health and Retirement Study) dataset we will use for the rest of the course.

Part A: Explore the codebook

WarningTask

Open the HRS codebook and answer the following:

  1. What does the variable srh measure, and what are its possible levels?
  2. Name two variables in the dataset that are stored as factor, and two that are stored as numeric.
  3. In 1–2 sentences, explain why this is a synthetic dataset rather than the real HRS data, and what that means for any patterns you might see in it.

Answer:

No answer given, for completion only.

Part B: A mini HRS sample

pacman::p_load(tidyverse)

mini_hrs <- data.frame(
  id     = c("R1", "R2", "R3", "R4", "R5"),
  age_yr = c(55, 62, 71, 48, 67),
  sex    = c("Female", "Male", "Male", "Female", "Male"),
  cesd   = c(2, 4, 6, 0, NA)
)

mini_hrs
  id age_yr    sex cesd
1 R1     55 Female    2
2 R2     62   Male    4
3 R3     71   Male    6
4 R4     48 Female    0
5 R5     67   Male   NA
WarningTask

Compare mini_hrs to the HRS codebook. In 2–3 sentences, describe what one row of mini_hrs represents, and name one way this mini sample is similar to (or different from) the full synthetic HRS dataset described in Lesson 11.

Answer:

Not given

Question 2: Functions

In Lesson 12, we learned that functions take the general form function_name(argument1 = value1, argument2 = value2, ...).

Part A: Call a function with arguments

WarningTask

Use the seq() function to generate a sequence of even numbers from 2 to 20, naming your arguments (from, to, by) explicitly. Assign the output to an object called evens, then print evens.

Answer:

The resulting sequence should look like:

 [1]  2  4  6  8 10 12 14 16 18 20

Part B: Use a function on your data

WarningTask

Using your mini_hrs data frame from Question 1, calculate the mean of the cesd column using the mean() function. Access the column with the $ operator (Lesson 13).

Answer:

No answer given, for completion only.

Part C: Look up a function’s help file

WarningTask

Pick any one function you used in Question 1 or 2 (for example, data.frame(), seq(), or mean()). Open its help file using ?. In 2–3 sentences, describe what you found: what are the function’s main arguments, and is there anything in the help file that surprised you or that you didn’t know before?

Answer:

Not given

Question 3: Objects

In Lesson 13, we learned about different types of R objects, both single pieces of data and collections of data.

Part A: Object types

WarningTask

Fill in the table below with an example value for each object class, and one sentence describing when you might use that type in the HRS dataset.

Answer:

Class Example value When might you see this in the real HRS dataset (see codebook)?
Double (dbl) 71 or 3.5 Continuous or count measures, like age_yr or a summed depression score
Character (chr)
Factor (fct)
Logical (lgl)

Not given

Part B: Check the class of variables

WarningTask

Using your mini_hrs data frame, use the class() function to check the object type of each of its four columns. Access each column with the $ operator.

Answer:

class(mini_hrs$id)
[1] "character"
class(mini_hrs$age_yr)
[1] "numeric"

Question 4: Common Errors

Even experienced R users run into errors constantly! Lesson 14 covered some of the most common ones.

Part A: Spot and fix the bugs

WarningTask

Each code chunk below contains one error. For each chunk, (1) identify what is causing the error, and (2) fix the code so it runs correctly.

# Chunk 1
mean(mini_hrs$Cesd)

Answer: cesd is case-sensitive, so it should be mini_hrs$cesd instead of mini_hrs$Cesd

# Chunk 2
avg_age <- mean(mini_hrs$age_yr

Answer: Not given

# Chunk 3
ages <- c(55, 62, 71, 48, 67)
avg <- mean("ages")

Answer: Not given

Part B: Read an error message

WarningTask

We ran the following code chunk and got the printed error. In 1–2 sentences, explain what the error message is telling you.

mini_hrs$age_yr + mini_hrs$id
Error in `mini_hrs$age_yr + mini_hrs$id`:
! non-numeric argument to binary operator

Answer:

Not given

Question 5: Getting Help

Knowing how to get unstuck is one of the most important skills in R. Lesson 15 covered strategies for getting help.

Part A: Search for your error

WarningTask

Take the error message from Question 4 Part B. Search for it online (Google, Stack Overflow, or another resource). Link to a resource you found helpful, and in 1–2 sentences summarize what it suggested.

Answer:

Not given

Part B: Ask a good question

WarningTask

Imagine you still can’t solve the error from Question 4 Part B, and you need to email your instructor for help. Write the email you would send. Your email should include the pieces of information a good help-seeking question needs (think about what you’d want to know if you were the one answering).

Answer:

Not given