This practice is mostly hands-on. You will import the full HRS dataset for the first time, export and re-import it to see how file type affects your data, rewrite nested code as pipes, and use some new operators and functions on the real dataset. You’ll get a little more practice with mean() and class() along the way — but the focus this week is on the new material.
Topics covered (Lessons 16-19):
Import and Export
Piping
Key operators and functions
Tidy data
TipNote on ordering
We’re tackling Import and Export (Lesson 18) first, even though it comes third in the lesson order, because we need the real HRS dataset loaded before we can practice piping and operators on it.
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_04.qmd (or lastname_firstinitial_practice_04.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
ImportantNeeded setup
Download hrs_data.rds (link on practice page) and place it in an appropriate folder
Install the rio and here packages if you need to
Questions
Question 1: Import and Export
In Lesson 18, we learned how to import and export data using file-specific functions and the rio package.
Part A: Import the HRS dataset
WarningTask
Load the tidyverse, rio, and here packages. Then use import() together with here() to import hrs_data.rds from your data folder. Assign it to an object called hrs_data. Use glimpse() (or tibble()) to take a look at its structure.
Part B: Export and re-import
WarningTask
Use export() from rio to save hrs_data as a .csv file named hrs_data_export.csv in the same folder as the hrs_data.rds file.
Use import() from rio to read hrs_data_export.csv back into R as a new object called hrs_data_csv.
Use class() to check the class of the sex column in bothhrs_data and hrs_data_csv. In 1-2 sentences, describe what you notice — is the class the same or different, and why might that be?
Write your code and answer here.
Part C: When to use which file type
WarningTask
In 1-2 sentences, explain when you would prefer to save a dataset as .rds versus .csv, based on what we discussed in Lesson 18.
Write your answer here.
Question 2: Piping
In Lesson 16, we learned that pipes (|>) let us write code that reads left to right, passing the output of one step into the next.
Rewrite this same calculation using the pipe (|>) instead of nesting. You will need the placeholder _ for at least one step, since paste0() does not take the piped input as its first argument. Assign the result to an object called cesd_summary and print it.
Part B: Write your own pipe
WarningTask
Using hrs_data$height, write a pipe that (1) calculates the standard deviation while handling any missing values, and (2) rounds that standard deviation to 3 decimal places. Assign the result to an object called height_summary and print it.
Question 3: Key operators and functions
In Lesson 17, we expanded on boolean operators and were introduced to several new functions.
Part A: Boolean operators and %in%
WarningTask
Using hrs_data$sex, answer the following with R code (don’t just eyeball the data frame):
Is "Female" one of the values in hrs_data$sex? Use %in%.
Write one logical statement that uses & to check whether the first respondent (hrs_data$age_yr[1]) is over 65 and the first respondent’s sex is "Female".
Part B: Statistical functions
WarningTask
Using hrs_data$cesd, calculate each of the following and print them: the standard deviation (sd()), the range (range()), and the mean rounded to the nearest whole number using round(). (Handle any missing values you find along the way.)
Part C: seq(), rep(), and sample()
WarningTask
Use set.seed(523) and then sample() to randomly draw 5 ids (without replacement) from hrs_data$id. Assign this to sampled_ids and print it.
In 1 sentence, explain why we use set.seed() before sample().
Question 4: Tidy data
In Lesson 19, we learned that tidy data follow three rules: each variable has its own column, each observation has its own row, and each value has its own cell.
Part A: Is it tidy?
WarningTask
Below is a small dataset showing the same information two different ways. For each table, state whether it is tidy or not, and explain why in 1-2 sentences (referencing the three rules above).
Table 1
id
age_yr
cesd
R1
55
2
R2
62
4
Table 2
id
variable
value
R1
age_yr
55
R1
cesd
2
R2
age_yr
62
R2
cesd
4
Write your answers here.
Part B: Is hrs_data tidy?
WarningTask
Look at hrs_data. In 1-2 sentences, explain whether it is tidy, and identify what one row and one column represent.
Write your answer here.
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.