Getting started

  1. Clone your repo appex05-[github_name] to create a new project in RStudio Cloud under the STA 199 class space.

  2. Configure git

    use_git_config("your name","your email")

Packages and Data

We’ll make use of the following packages.



Task 1

Write a function called center_measures that has one argument - x a numeric vector. The function should return the mean and median of x. Below is some sample code.

nums <- c(10, 5, -1, 0, 4)
c(mean(nums), median(nums))
#> [1] 3.6 4.0
center_measures <- function() {

Task 2

Turn the following code into a function. How many arguments does it need? What is a good name for this function? Assume x will be a nonnegative numeric vector.

x / sum(x)

Task 3

congress <- read_csv("")

Write a for loop to compute the number of unique values in each column of tibble congress. Try to stay within the tidyverse syntax as much as possible.

You may find function length() useful. Below are some examples of it in action.

#> [1] 10
length(c("a", "ab", "abc", "abcd"))
#> [1] 4
length(seq(from = 2, to = 10, by = 2))
#> [1] 5

Task 4

Function plot_congress() is similar to the function in the slides. I added some annotation so we know the year and legislative branch.

Run the example below to see the change that was made with annotate().

plot_congress(year = 2019, leg_branch = "house")