This practice is mostly hands-on. You will get familiar with the HRS dataset we will use for the rest of the course, practice calling functions and reading their help files, work with different types of R objects, and build up your skills for spotting and fixing common errors — all skills you will rely on every week.
Topics covered (Lessons 11-15):
Introduction to HRS dataset
Functions
Objects
Common errors
Getting help
Directions
Download this practice .qmd from the course GitHub repository, rename it, and save it in your course folder.
Rename the file to Lastname_Firstinitial_Practice_03.qmd (or lastname_firstinitial_practice_03.qmd), replacing with your actual last name and first initial.
Save it inside your practice folder. You may create a practice_04 subfolder if you prefer.
TipHow to use this file
The boxes with the yellow stripe explain what to do. Keep these in your submitted file.
Do all your work below and outside of the yellow-striped boxes.
You will need to create code chunks within this document to run your R code.
Please delete the following sections before submitting:
Overview
Directions
Tip boxes (green stripe)
Caution boxes / Needed Steps (orange/red stripe)
Submission checklist
Note on AI usage
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.
What does the variable srh measure, and what are its possible levels?
Name two variables in the dataset that are stored as factor, and two that are stored as numeric.
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.
Write your answers here.
Part B: A mini HRS sample
TipNote
We have not yet learned how to import outside data files into R (that’s coming in a future lesson!). For now, here is a small, made-up sample of 5 “respondents” with a few variables from the codebook, built directly in R. Run the chunk below so mini_hrs is available for the rest of this practice — you don’t need to change anything in it.
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.
Write your answer here.
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.
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).
TipTip
Look closely at the cesd column in mini_hrs — one value is missing (NA). You’ll need to look at the help file for mean() to figure out which argument handles missing values, or your result will print as NA.
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?
Write your answer here.
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.
Class
Example value
When might you see this in the real HRS dataset (see codebook)?
Double (dbl)
Character (chr)
Factor (fct)
Logical (lgl)
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.
For each column, state whether the class is what you would have expected before running class().
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.
Common culprits include: capitalization/spelling of variable or object names, unmatched parentheses, missing commas, quoting something that shouldn’t be quoted (or vice versa), and forgetting to load a package.
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
Explain what the error message means:
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.
Link and summary here:
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).
Write your email here.
TipTip
Refer back to Lesson 15 for what makes a question easy for someone else to help with — for example, showing your actual code and the exact error message, rather than just saying “it doesn’t work.”
Submission Checklist
If having a checklist helps you stay organized, check off each item below as you complete it. You do not need to submit this checklist.
Note on AI usage
I used GenAI (Claude) to help me draft this practice assignment. It helped me brainstorm ideas for the assignment, and I directed it to help you complete the needed tasks that I showed in the lessons.