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).

  1. 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."))
    
  2. 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
  1. Write a function called tree_volume_calc() that calculates the canopy volume for the Acacia species in the dataset. To do so, use an if statement in combination with the str_detect() function from the stringr R package. The code str_detect(SPECIES, "Acacia") will return TRUE if the string stored in this variable contains the word “Acacia” and FALSE if 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)

  2. Expand this function to additionally calculate canopy volumes for other types of trees in this dataset by adding if/else statements 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)

  3. Now get the canopy volumes for all the trees in the tree_data dataframe and add them as a new column to the data frame. You can do this using tree_volume_calc() and either mapply() or using dplyr with rowwise and mutate.


Exercise 3: Tree growth

  1. Write a function named get_growth() that takes two inputs, a vector of sizes and a vector of years, 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 running get_growth(c(40.2, 42.6, 46.0), c(2020, 2021, 2022)).

  2. Use dplyr and the function you created above to get the growth for each individual tree along with information about the TREATMENT for that tree. Trees are identified by a unique value in the ORIGINAL_TAG column. Don’t include information for cases where a TREATMENT is not known (e.g., where it is NA).

  3. Using ggplot and the output from (2) make a histogram of growth rates for each TREATMENT, whith each TREATMENT in it’s own facet. Use geom_vline() to add a vertical line at 0 to help indicate which trees are getting bigger vs. smaller. Include good axis labels.