Learning Objectives

Following this assignment students should be able to:

  • use, modify, and write custom functions
  • use the output of one function as the input of another

Reading

Lecture Notes

  1. Functions

Exercises

  1. Writing Functions (25 pts)

    Write a function that converts pounds to grams (there are 453.592 grams in one pound). It should take a value in pounds as the input and return the equivalent value in grams (i.e., the number of pounds times 453.592). Use that function to calculate how many grams there are in 3.75 pounds.

    [click here for output]
  2. Use and Modify (25 pts)

    The length of an organism is typically strongly correlated with its body mass. This is useful because it allows us to estimate the mass of an organism even if we only know its length. This relationship generally takes the form:

    Mass = a * Lengthb

    Where the parameters a and b vary among groups. This allometric approach is regularly used to estimate the mass of dinosaurs since we cannot weigh something that is only preserved as bones.

    The following function estimates the mass of an organism in kg based on its length in meters for a particular set of parameter values, those for Theropoda (where a has been estimated as 0.73 and b has been estimated as 3.63; Seebacher 2001).

    get_mass_from_length_theropoda <- function(length){
      mass <- 0.73 * length ^ 3.63
      return(mass)
    }
    
    1. Add a comment to this function so that you know what it does.
    2. Use this function to print out the mass of a Spinosaurus that is 16 m long based on its reassembled skeleton.
    3. Create a new version of this function called get_mass_from_length() that estimates the mass of an organism in kg based on its length in meters by taking length, a, and b as parameters. This lets us pass the function all 3 values that it needs to estimate a mass as parameters, which makes it much easier to reuse for all of the non-theropod species. Use this new function to estimate the mass of a Sauropoda (a = 214.44, b = 1.46) that is 26 m long.
    [click here for output]
  3. Default Arguments (25 pts)

    This is a follow up to Use and Modify.

    Allowing a and b to be passed as arguments to get_mass_from_length() made the function more flexible, but for some types of dinosaurs we don’t have specific values of a and b and so we have to use general values that can be applied to a number of different species.

    Set default values for a and b of a = 39.9 and b = 2.6 (the average values from Seebacher 2001).

    1. Use this function to estimate the mass of a Sauropoda (a = 214.44, b = 1.46) that is 22 m long (by setting a and b when calling the function).
    2. Use this function to estimate the mass of a dinosaur from an unknown taxonomic group that is 16m long (by not setting a and b so that the default values are used).
    [click here for output]
  4. Combining Functions (25 pts)

    This is a follow up to Use and Modify.

    Measuring things using the metric system is the standard approach for scientists, but when communicating your results more broadly it may be useful to use different units (at least in some countries). Write a function that converts kilograms into pounds (there are 2.205 pounds in a kilogram). Use that function along with your get_mass_from_length() function from Use and Modify to estimate the weight, in pounds, of a 12 m long Stegosaurus. In Stegosauria, a has been estimated as 10.95 and b has been estimated as 2.64 (Seebacher 2001).

    [click here for output]
  5. Climate Space Rewrite (optional)

    This is a follow up to Climate Space.

    Producing a plot of occurrences on the available climate space for each of the three species required a lot of repetition of very similar code. Whenever this happens, it is usually an indication that a function could be used instead. Such functions reduce the repetition in producing the three species plots, which enables you to save time and prevent errors by not having to rewrite the same code multiple times.

    1. Create a function to download occurrence data and extract the corresponding climate data, which should return a dataset of all the bioclim variables for a single species. Because the latitude and longitude columns for each occurrence dataset have different names you can select and set them to the same name using the column index, instead of the column name, to get only those columns (e.g., select(longitude = 2, latitude = 3).

    2. Create a second function for plotting the occurrences for a single species onto the available climate space, then use this function to generate separate plots for each of the three tree species.

    [click here for output] [click here for output] [click here for output]

Assignment submission & checklist