pacman::p_load(
tidyverse,
rio,
here
)
1hrs_data <- import(here("data", "hrs_data.rds"))- 1
- This file path is specific to my folder set-up.
PUBH 523/623
Nicky Wakim
July 29, 2026
pacman::p_load(
tidyverse,
rio,
here
)
1hrs_data <- import(here("data", "hrs_data.rds"))dplyr BasicsIn Lesson 20, we learned that dplyr functions are “verbs” that take a data frame in and return a data frame out, and that they can be grouped into four categories based on what they operate on: rows, columns, groups, and joins.
In 1 sentence, describe the general input and output of most dplyr functions. What type of object typically goes in, and what type of object comes out?
Answer:
Not given
For each function below, fill in the table with:
hrs_data (for example: the same number of rows but fewer columns, fewer rows but all the same columns, the rows reordered, etc.)| Function | Rows, Columns, or Groups (not yet covered)? | What does it do? | Expected output on hrs_data |
|---|---|---|---|
filter() |
|||
select() |
|||
mutate() |
|||
arrange() |
|||
distinct() |
|||
relocate() |
|||
rename() |
|||
group_by() |
|||
summarize() |
Answer:
Example of mutate:
| Function | Rows, Columns, or Groups (not yet covered)? | What does it do? | Expected output on hrs_data |
|---|---|---|---|
filter() |
|||
select() |
|||
mutate() |
Columns | Creates a new column, or changes the values in an existing one | Same rows, same original columns plus any new column(s) added |
arrange() |
|||
distinct() |
|||
relocate() |
|||
rename() |
|||
group_by() |
|||
summarize() |
In Lesson 21, we learned three functions for transforming rows: filter(), arrange(), and distinct().
filter()Using hrs_data:
Use filter() to keep only respondents whose cond_count (total number of chronic conditions) is 5 or more. Assign this to hrs_01_filter_conditions, and use nrow() to report how many respondents that is.
Answer:
Here’s an example to show you the pattern — the rest of your answers should follow this same style, with code chunks and a #| ... chunk options as needed.
[1] 97
# A tibble: 97 × 32
HHID pn id BIRTHYR BIRTHMO BIRTHDATE proxy coupled sex age_mo age_yr
<fct> <fct> <chr> <dbl> <dbl> <dbl> <fct> <fct> <fct> <dbl> <dbl>
1 5511… 010 5511… 1940 10 -7170 Resp… Not a … Fema… 992 82
2 5509… 020 5509… 1967 6 2722 Resp… Couple… Fema… 665 55
3 5599… 010 5599… 1960 11 319 Resp… Not a … Fema… 757 63
4 5501… 020 5501… 1953 6 -2361 Resp… Couple… Male 838 69
5 5566… 010 5566… 1937 8 -7444 Resp… Couple… Fema… 1013 84
6 5511… 020 5511… 1973 2 4763 Resp… Couple… Fema… 607 50
7 5599… 010 5599… 1950 7 -3548 Resp… Not a … Fema… 868 72
8 5510… 020 5510… 1957 3 -1022 Resp… Couple… Male 801 66
9 5569… 010 5569… 1945 2 -5374 Resp… Not a … Male 935 77
10 5572… 010 5572… 1971 7 4213 Resp… Not a … Fema… 618 51
# ℹ 87 more rows
# ℹ 21 more variables: ed <dbl>, degree <fct>, race_original <fct>,
# smoke_ever <fct>, smoke_now <fct>, drink <fct>, height <dbl>, srh <fct>,
# act_vig <fct>, bp <fct>, diab <fct>, cancer <fct>, lung <fct>, hrt <fct>,
# strk <fct>, psych <fct>, sleep <fct>, arth <fct>, cond_count <dbl>,
# cesd <dbl>, income <dbl>
Use filter() to keep only respondents whose srh (self-rated health) is exactly "Poor". Assign this to hrs_02_filter_srh.
Remember: if you misspell or mis-capitalize a value for a categorical/factor variable, filter() will not throw an error — it will just silently return 0 rows. Always check your output!
Answer:
Not given
arrange()Sort hrs_data by cesd in descending order first, and within ties, by age_yr in ascending order. Assign the result to hrs_03_arrange, and print it using tibble().
Answer:
Not given
In 1 sentence, describe what would change about the output if you arranged by age_yr first and cesd second instead.
Answer:
Not given
distinct()Use distinct() on degree and race_original together to see how many unique combinations of these two variables exist in hrs_data. Assign this to hrs_04_distinct, and print it using tibble().
Answer:
The resulting distinct combinations would look like this:
degree race_original
1 <NA> <NA>
2 High school diploma White/Caucasian
3 Master's degree White/Caucasian
4 Master's degree Black/African American
5 Professional degree (Ph.D./M.D./JD) White/Caucasian
6 None Black/African American
7 Associate's degree Black/African American
8 Professional degree (Ph.D./M.D./JD) Other
9 GED White/Caucasian
10 High school diploma Black/African American
11 None White/Caucasian
12 Associate's degree White/Caucasian
13 None Other
14 Master's degree Other
15 Associate's degree Other
16 Professional degree (Ph.D./M.D./JD) Black/African American
17 GED Black/African American
18 Bachelor's degree White/Caucasian
19 None <NA>
20 Bachelor's degree Other
21 Bachelor's degree Black/African American
22 GED Other
23 High school diploma Other
24 <NA> Other
25 Bachelor's degree <NA>
26 High school diploma <NA>
27 Master's degree <NA>
28 Associate's degree <NA>
29 <NA> Black/African American
Use distinct() on degree again, but this time with .keep_all = TRUE, so you keep the full row (all columns) for the first respondent found at each unique degree value. Assign this to hrs_05_distinct_all.
Answer:
Not given
In Lesson 22, we learned four functions for transforming columns: select(), relocate(), mutate(), and rename().
select() and relocate()Use select() to keep only id, age_yr, ed, degree, income, cesd, and srh from hrs_data. Assign this to hrs_06_vars_subset.
Answer:
Not given
Starting from hrs_06_vars_subset, use relocate() to move srh so that it comes right after id. Assign this to hrs_07_reloc, and print it using tibble().
Answer:
Not given
mutate()Using hrs_data, create a new variable called height_ft that converts height (recorded in meters) into feet, using the conversion 1 meter = 3.28084 feet. Round height_ft to 1 decimal place using round() inside your mutate() call. Assign the result to hrs_08_mutate, and use relocate() so that height_ft sits right next to height for easy comparison. Print it using tibble().
Remember, inside mutate() you use a single =, not <- or ==, to define your new column.
rename()Using hrs_data, rename cesd to depression_score and srh to self_rated_health. Assign the result to hrs_09_rename, and print names(hrs_09_rename) to confirm the change.
Answer:
Not given
In Lesson 23, we covered more advanced techniques: combining conditions with &, |, and %in%; checking our work; creating variables with case_when(); and working with factors.
Before writing any code: do you expect more respondents to have diabetes AND high blood pressure (bp), or diabetes OR high blood pressure? Explain your reasoning in 1 sentence.
Answer:
Not given
Use filter() with & to find respondents who have diabetes and have high blood pressure. Assign this to hrs_10_filter_diab_bp.
Use filter() with | to find respondents who have diabetes or have high blood pressure. Assign this to hrs_11_filter_diab_or_bp.
Answer:
Not given
Print hrs_10_filter_diab_bp and hrs_11_filter_diab_or_bp using tibble(), and reference the row count shown at the top of each output to compare how many respondents are in each data frame. Then use distinct() on diab and bp within each object to see which combinations of diab and bp are actually present in each dataset. Does this match your prediction from Task 1?
Answer:
Not given
Now combine multiple logic statements in a single filter(): find respondents who have diabetes and have high blood pressure, or who have more than 5 chronic conditions. Assign this to hrs_12_filter_combo. Think carefully about where your parentheses go.
Answer:
Not given
case_when()Using hrs_data, create a new variable called income_cat with 4 categories based on income:
"<25k" if income is less than $25,000"25k-50k" if income is at least $25,000 but less than $50,000"50k+" if income is $50,000 or moreNA if income is missing (use .default for this case)Then use fct() to convert income_cat into a factor. Assign your final result to hrs_13_case_when, and check your work using count().
For the middle category, you’ll need an & statement inside case_when(), similar to how we combined conditions in Part A.
Check the current class of hrs_data$srh using class().
Use fct_relevel() to reorder the levels of srh into descending order from “Excellent” to “Poor.” Call the new column srh_releveled, and assign the result to hrs_14_relevel. Confirm the new order with levels().
If you misspell one of the levels inside fct_relevel(), R will not throw an error — it will silently drop that level to the bottom of the order (and print a warning in the console). Double-check your spelling against the original values in srh!
Answer:
Not given
Now suppose you had instead written fct_relevel(srh, "Excellent", "Very good", "Good", "Fair", "Poor") — with a lowercase “g” in "Very good". Run this version and look at the warning message R gives you. In 1-2 sentences, explain what the warning is telling you and why it happens.
Answer:
Not given
Use select() with contains() to grab all columns in hrs_data whose name contains "smoke". Assign this to hrs_15_select_contains, and print it using tibble().
Use select() with where(is.factor) to grab all factor columns in hrs_data, and pipe the result into names() so you just see the column names.
Answer:
Not given