Homework 8

Instructions

Answer each of the following questions. Be sure to display all your code in the rendered version (use echo: true throughout).

Exercises

Question 1:

Given a vector of birthdates1, write a function to compute the age in years2.

Question 2:

For each of the following functions list all arguments that use tidy evaluation and describe whether they use data-masking or tidy-selection: distinct(), count(), group_by()3, rename_with(), slice_min(), slice_sample().

Question 3:

Build up a rich plotting function by incrementally implementing each of the steps below:

  • Draw a scatterplot given dataset and x and y variables.
  • Add a line of best fit (i.e. a linear model with no standard errors)4.
  • Add a title that includes the names of the variables used.

Test your function with the built-in cars, swiss, and iris datasets.

Footnotes

  1. Assume mdy format for this vector.↩︎

  2. Depending upon how you want to solve this problem, the lubridate functions today() and time_length() could be useful here :)↩︎

  3. Note: group_by() doesn’t list what type of tidy evaluation it uses for its ... argument but you can find the answer in the introduction to this article.↩︎

  4. geom_smooth() has a method and se argument that will be necessary to specify here.↩︎