Homework Day 1: Functions in R

Instructions

  • Open a new file called “functions-after.Rmd” that is saved in your “documents” folder.
  • Write the code to solve the following exercises in R chunks.
  • Add comments to each line of code explaining with your own words what the code is doing.
  • Once you are finished, knit to PDF.
  • Git add, commit and push the new files (PDF and Rmd) to your remote repository.

Exercise 1: A function for the Portal data set

  1. If surveys.csv, species.csv, and plots.csv are not available in your workspace download them, and load them into R using read.csv().
  2. Combine the surveys and species tables into a single data frame.
  3. Write a function that:
    • Takes three arguments - a data frame (the combined table created before), a genus name, and a species name,
    • Uses functions from the dplyr package to produce a data frame with two columns: year and count, where count is the number of individuals (i.e., the number of rows) for the species indicated by genus and species in that year,
    • Returns the resulting data frame. Note that this data frame is a time series by year.
  4. Use your function to get the data frame of time series by year for the genus Dipodomys and the species merriami and then make a graph of this time series using ggplot that has year on the x axis, count on the y axis, and displays the data as points.
  5. Use your function to get the data frame time series by year for the genus Chaetodipus and species penicillatus and then make a graph of this time-series using ggplot that has year on the x axis, count on the y axis, and displays the data as blue points (with size = 1) connected by blue lines (with size = 2). Change the x axis label to Year and the y axis label to Number of Individuals.

Homework Day 2: Making choices in R

Instructions

  • Open a new file called “choices-after.Rmd” that is saved in your “documents” folder.
  • Write the code to solve the following exercises in R chunks.
  • Add comments to each line of code explaining with your own words what the code is doing.
  • Once you are finished, knit to PDF.
  • Git add, commit and push the new files (PDF and Rmd) to your remote repository.