This practice shifts from transforming the HRS dataset to describing it. You will practice quickly browsing a dataset, building frequency and summary tables, and comparing summary statistics across groups. Along the way you’ll get a little more practice with select() and the pipe (|>) from previous weeks — but the focus this week is on the new summarization functions.
Topics covered (Lessons 24-27):
Data summarization: Intro
Data summarization: Quick summaries
Data summarization: Tables
Data summarization: Grouped summaries
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_06.qmd (or lastname_firstinitial_practice_06.qmd), replacing with your actual last name and first initial.
Save it inside your practice folder. You may create a practice_06 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
You should already have hrs_data.rds from Practice 4 (link on the practice page). Place it in your data folder if you haven’t already.
Load the tidyverse, here, and rio packages, plus three new ones for this week: skimr, janitor, rstatix, gtsummary, and gt.
Remember, you can use pacman::p_load() to install and load packages in one step.
Questions
Question 1: Data Summarization: Intro
In Lesson 24, we saw that there are many ways to summarize data in R, and that the right tool often depends on whether a variable is categorical or numeric.
Part A: Categorical vs. numeric
WarningTask
In 1-2 sentences, explain why we might need different summary tools for categorical data versus numeric data. What does each type of tool tend to focus on?
Write your answer here.
Part B: Sort the functions
WarningTask
For each function below, fill in the table with:
Which package it comes from
Whether it works on categorical, numeric, or both types of data
What you’d use it for (a quick browse of a whole dataset, a custom/pointed summary, a grouped summary, or a presentation-ready table — more than one may apply)
What it does, in your own words
Function
Package
Categorical, Numeric, or Both?
What would you use it for?
What does it do?
summary()
skim()
get_summary_stats()
summarise()
tabyl()
tbl_summary()
Question 2: Data Summarization: Quick summaries
In Lesson 25, we covered three functions for quickly browsing a dataset: summary(), skim(), and get_summary_stats().
Part A: summary()
WarningTask 1
Use summary() on all of hrs_data. Look through the output but do not assign it to anything. Do you notice anything interesting about the summary statistics of HHID (our household identifier) and id (our observation identifier)?
Write your answer here.
WarningTask 2
Now use summary() on just hrs_data$income. What is the median income?
TipTip
summary() treats categorical (factor) and numeric columns differently — categorical columns show counts per category, while numeric columns show min/median/mean/max, etc.
Write your answer here.
Part B: skim()
WarningTask 1
Use skim() on all of hrs_data. What variables have missing categories from the top_counts?
Write your answer here.
WarningTask 2
Using select(), first keep only cesd, cond_count, and income from hrs_data, then pipe the result into skim(). Based on the output, which of these three variables has the most missing values, and how many?
Write your answer here.
Part C: get_summary_stats()
WarningTask 1
Use get_summary_stats() on hrs_data to get the "common" summary statistics for every numeric variable. Assign the result to hrs_01_summary_stats.
What is the IQR for ed?
Write your answer here.
WarningTask 2
Now use get_summary_stats() again, but this time restrict it to just age_yr, cesd, and income, and set type = "mean_sd". Assign the result to hrs_02_summary_stats_meansd.
What is the standard deviation of cesd?
Write your answer here.
TipTip
get_summary_stats() only works on numeric variables — any categorical variables you feed it will be silently dropped.
Question 3: Data Summarization: Tables
In Lesson 26, we covered two functions for building summary tables: tabyl() and tbl_summary(), plus gt() for presentation.
Part A: tabyl()
WarningTask 1
Use tabyl() to build a frequency table of srh (self-rated health). Assign the result to hrs_03_tabyl_srh.
WarningTask 2
Use tabyl() to cross-tabulate srh by sleep. Assign the result to hrs_04_tabyl_cross.
Part B: adorn_*()
WarningTask
Starting from hrs_04_tabyl_cross, add a totals row, convert the counts to column percentages, format the percentages to 1 decimal place, and add the raw counts back in front of the percentages. Assign the result to hrs_05_tabyl_formatted and display the table.
TipTip
Order matters when chaining adorn_*() functions together: tabulate → add totals → convert to percentages → format as percents → add counts back.
Part C: tbl_summary()
WarningTask 1
Using select(), keep only age_yr, act_vig, bp, lung, cesd, and height from hrs_data, then pipe the result into tbl_summary(). Assign the result to hrs_06_tbl_summary.
How many missing values are there for “Ever Had High Blood Pressure”?
Do you notice any difference between the summary statistics for cesd using tbl_summary() and skim()?
Write your answer here.
WarningTask 2
Starting over from hrs_data, build another tbl_summary() with just age_yr and height, but this time show {mean} ({sd}) for both variables instead of the defaults, and give height the display label "Self-Reported Height (m)" to make sure readers know height is measured in meters. Assign the result to hrs_07_tbl_summary_custom and display the table.
TipTip
Since both variables are numeric, we can use the following input to make ALL numeric variables show mean and standard deviation:
`statistic = all_continuous() ~ "{mean} ({sd})"`
Part D: gt()
WarningTask
Take hrs_03_tabyl_srh from Part A and pipe it into gt(), then use tab_header() to give the table the title "Self-Rated Health". Assign the result to hrs_08_gt.
Question 4: Data Summarization: Grouped summaries
In Lesson 27, we covered how to compare summary statistics across groups using group_by() with summarise(), skim(), and get_summary_stats(), as well as the by = argument in tbl_summary().
Part A: summarise() with group_by()
WarningTask
Group hrs_data by degree, then use summarise() to calculate the mean and standard deviation of income for each group (remember to handle missing values). Assign the result to hrs_09_grouped_summarise.
Which degree group has the highest mean income?
Write your answer here.
Part B: skim() grouped
WarningTask
Group hrs_data by sex, use select() to keep only cesd and cond_count, and then pipe the result into skim(). Look through the output but do not assign it to anything.
Does the average cesd score differ between males and females?
Write your answer here.
Part C: get_summary_stats() grouped
WarningTask
Group hrs_data by bp (high blood pressure), then use get_summary_stats() on age_yr and cond_count with type = "common". Assign the result to hrs_10_grouped_stats.
How does mean age_yr compare between those with and without high blood pressure?
Write your answer here.
Part D: tbl_summary() with by =
WarningTask
Using select(), keep age_yr, cesd, srh, and diab from hrs_data, then use tbl_summary() to stratify the table by diabetes status. Assign the result to hrs_11_tbl_summary_grouped and display the table.
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.