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.
A function for the Portal data set
- If surveys.csv, species.csv, and plots.csv are not available in your workspace download them, and load them into R using
read.csv()
. - Combine the
surveys
andspecies
tables into a single data frame. - Write a function that:
- Takes three arguments - a data frame (the combined table created before), a
genus
name, and aspecies
name, - Uses functions from the
dplyr
package to produce a data frame with two columns:year
andcount
, wherecount
is the number of individuals (i.e., the number of rows) for the species indicated bygenus
andspecies
in thatyear
, - Returns the resulting data frame. Note that this data frame is a time series by year.
- Takes three arguments - a data frame (the combined table created before), a
- Use your function to get the data frame of time series by year for the
genus
Dipodomys and thespecies
merriami and then make a graph of this time series usingggplot
that hasyear
on the x axis,count
on the y axis, and displays the data as points. - Use your function to get the data frame time series by year for the
genus
Chaetodipus andspecies
penicillatus and then make a graph of this time-series usingggplot
that hasyear
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 toYear
and the y axis label toNumber 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.