Homework
Instructions
- (20 points) Open a new file called “loops-after.Rmd” and save it in your “documents” folder.
- (20 points) Write the code to solve the following exercises in R chunks.
- (20 points) Add comments to each line of code explaining with your own words what the code is doing.
- (20 points) Once you are finished, knit to PDF.
- (20 points) Git add, commit and push the new files (PDF and Rmd) to your remote repository.
Exercise 1: The data set
The UHURU experiment in Kenya has conducted a survey of Acacia and other tree species in ungulate exclosure treatments.
Each of the individuals surveyed were measured for tree height (HEIGHT), circumference (CIRC) and canopy size in two directions (AXIS_1 and AXIS_2).
-
Read the UHURU tree data available for download in a tab delimited (
"\t") format using the following code:tree_data <- read.csv("https://ndownloader.figshare.com/files/5629536", sep = '\t', na.strings = c("dead", "missing", "MISSING", "NA", "?", "3.3.")) -
What is the code doing? Explain the meaning of each argument and how the values used for each argument afect the outcome.
Exercise 2: Tree volumes
You want to estimate the crown volumes for the different tree species and have developed equations for species in the Acacia genus:
volume = 0.16 * HEIGHT^0.8 * pi * AXIS_1 * AXIS_2
and the Balanites genus:
volume = 1.2 * HEIGHT^0.26 * pi * AXIS_1 * AXIS_2
For all other genera you’ll use a general equation developed for any tree:
volume = 0.5 * HEIGHT^0.6 * pi * AXIS_1 * AXIS_2
-
Write a function called
tree_volume_calc()that calculates the canopy volume for the Acacia species in the dataset. To do so, use anifstatement in combination with thestr_detect()function from thestringrR package. The codestr_detect(SPECIES, "Acacia")will returnTRUEif the string stored in this variable contains the word “Acacia” andFALSEif it does not. This function will have to take the following arguments as input: SPECIES, HEIGHT, AXIS_1, AXIS_2. Then run the following line:tree_volume_calc("Acacia_brevispica", 2.2, 3.5, 1.12) -
Expand this function to additionally calculate canopy volumes for other types of trees in this dataset by adding
if/elsestatements and including the volume equations for the Balanites genus and other genera. Then run the following lines:tree_volume_calc("Balanites", 2.2, 3.5, 1.12)tree_volume_calc("Croton", 2.2, 3.5, 1.12) -
Now get the canopy volumes for all the trees in the
tree_datadataframe and add them as a new column to the data frame. You can do this usingtree_volume_calc()and eithermapply()or usingdplyrwithrowwiseandmutate.
Exercise 3: Tree growth
-
Write a function named
get_growth()that takes two inputs, a vector ofsizesand a vector ofyears, and calculates the average annual growth rate. Pseudo-code for calculating this rate is(size_in_last_year - size_in_first_year) / (last_year - first_year). Test this function by runningget_growth(c(40.2, 42.6, 46.0), c(2020, 2021, 2022)). -
Use
dplyrand the function you created above to get the growth for each individual tree along with information about theTREATMENTfor that tree. Trees are identified by a unique value in theORIGINAL_TAGcolumn. Don’t include information for cases where aTREATMENTis not known (e.g., where it isNA). -
Using
ggplotand the output from (2) make a histogram of growth rates for eachTREATMENT, whith eachTREATMENTin it’s own facet. Usegeom_vline()to add a vertical line at 0 to help indicate which trees are getting bigger vs. smaller. Include good axis labels.
