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 1, 2026
In Lesson 18, we learned how to import and export data using file-specific functions and the rio package.
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.
Answer:
pacman::p_load(
tidyverse,
rio,
here
)
1hrs_data <- import(here("data", "hrs_data.rds"))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.import() from rio to read hrs_data_export.csv back into R as a new object called hrs_data_csv.class() to check the class of the sex column in both hrs_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?Answer:
Not given
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.
Answer:
Not given
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.
Here is a chunk of nested code:
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.
Using hrs_data$height, write a pipe that (1) calculates the mean while handling any missing values, and (2) rounds that mean to 1 decimal place. Assign the result to an object called height_summary and print it.
Answer:
Not given
In Lesson 17, we expanded on boolean operators and were introduced to several new functions.
%in%Using hrs_data$sex, answer the following with R code (don’t just eyeball the data frame):
"Female" one of the values in hrs_data$sex? Use %in%.& to check whether the first respondent (hrs_data$age_yr[1]) is over 65 and the first respondent’s sex is "Female".Answer:
Not given
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.)
Answer:
Standard deviation result:
[1] 2.128984
seq(), rep(), and sample()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.set.seed() before sample().Answer:
Sampled ID’s:
[1] "559315_010" "555653_010" "559806_020" "553623_010" "553793_010"
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.
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 |
Answer:
Table 1 is tidy. Table is not tidy.
hrs_data tidy?Look at hrs_data. In 1-2 sentences, explain whether it is tidy, and identify what one row and one column represent.
Answer:
Not given