Homework 3
For this homework you will create a github repo, set up github pages, clone the repo to your computer as an
R project, create a .qmd file, and push those changes back to github to create a webpage! You’ll submit the
link to your github pages site (the one that looks like a nice website). Please see HW 1 for full instructions
on that process!
If you were unable to get RStudio and github connected, try to set up a meeting with Dr. Post or Gabby to
get that figured out!
Setting up your environment:
Continue the example from the previous homework. There is a .rda file (basically a file with saved R objects)
available in the homework activity on Moodle. This has a version of the list you created in the last part of
the previous homework.
You should download this file, put it in your working directory and read it in via the code below:
load("hw2_list.rda")
You should see bp_list in your environment!
Task 1: Control Flow Practice
1. Suppose we want to characterize the post-treatment (or placebo) blood pressure measurement as
optimal (≤ 120), borderline (120 < bp ≤ 130), and high (> 130). First, create a new column
in each data frame from above called status. You can do this via
your_df$status <- character(20) #or 10 depending on number of observations
Note: You want to do this additional column to the data frames that are stored in your list not the
original data frames you had in your environment. Although R uses smart ways to avoid using excess
memory, it doesn’t overwrite the data frame stored in your list if you modify the original object.
2. For the non-placebo data frame (within the list), create a for loop and use if/then/else logic to
create the status column’s values.
3. Repeat for the placebo data frame (within the list).
Task 2: Function Writing
Continue the previous example. Suppose you would eventually have many data sets in the form of the two
above. You want to write a function to do some things for you quickly.
1. Write a function that
1
• takes in a list with two data frames in it (a treatment and a placebo data frame) as an argument.
Give no default value.
• takes in an R function (that would find a summary of a numeric column) with the default value
being set to "mean" (notice this is a quoted string).
• Finds the statistic of interest (as defined by the user input) for the pre, post, and diff columns
of both data frames.
– Use my_fun <- get(stat) within the function to get the function from the quoted string.
• These six values should then be returned as a named list with meaningful names - this is a
somewhat challenging part!
– I’m going to let you consider what to do but you might create a vector of names that is
created dynamically based on the statistic passed, create a vector with the actual statistic
values, and then assign names() to your vector. Then return that (an atomic vector (our
standard 1D vector) with names can be returned instead of a list).
• Finally, apply your function to you list of data frames from previous. Use it without specifying
your statistic, with specifying your statistic as "var", "sd", "min", and "max".
• You can render the document to check things are looking good. Make sure that all code chunks show
(and are evaluated). Use headings to separate the sections. Write text before each code chunk
explaining what you are trying to do. Use markdown where appropriate (to create lists, bold things,
etc.).
• Render things appropriately to create your website. Copy the link to that nicely rendered site
and that is what you’ll turn in for this assignment!