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
  2. Googling for Help
  3. Coding style

Exercises

  1. Writing Functions (15 pts)

    1. Copy the following function (which converts weights in pounds to weights in grams) into your assignment and replace the ________ with the variable names for the input and output.

    convert_pounds_to_grams <- function(________) {
        grams = 453.6 * pounds
        return(________)
    }
    

    Use the function to calculate how many grams there are in 3.75 pounds.

    2. Copy the following function (which converts temperatures in Fahrenheit to temperatures in Celsius) into your assignment and replace the ________ with the needed commands and variable names so that the function returns the calculated value for Celsius.

    convert_fahrenheit_to_celsius <- ________(________) {
        celsius = (fahrenheit - 32) * 5 / 9
        ________(________)
    }
    

    Use the function to calculate the temperature in Celsius if the temperature in Fahrenheit is 80°F.

    3. Write a function named double that takes a number as input and outputs that number multiplied by 2. Run it with an input of 512.

    4. Write a function named prediction that takes three arguments, x, a, and b, and returns y using y = a + b * x (like a prediction from a simple linear model). Run it with x = 12, a = 6, and b = 0.8.

    Expected outputs for Writing Functions: 1
  2. Use and Modify (15 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 * length^b

    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. Use this function to print out the mass of a Theropoda that is 16 m long based on its reassembled skeleton.
    2. Create a new version of this function called get_mass_from_length() that takes length, a and b as arguments and uses the following code to estimate the mass mass <- a * length ^ b. Use this function to estimate the mass of a Sauropoda (a = 214.44, b = 1.46) that is 26 m long.
    Expected outputs for Use and Modify: 1
  3. Writing Functions 2 (15 pts)

    1. Copy the following function (which converts weights in pounds to weights in grams and rounds them) into your assignment. Replace the ________ with the variable names for the input and output. Replace __ with a number so that by default the function will round the output to one decimal place.

    convert_pounds_to_grams <- function(________, numdigits = __) {
      grams <- 453.6 * pounds
      rounded <- round(grams, digits = numdigits)
      return(________)
    }
    

    Use the function to calculate how many grams there are in 4.3 pounds using the default for the number of decimal places.

    2. Write a function called get_height_from_weight that takes three arguments, weight, a, and b, and returns an estimate of height using height = a * weight ^ b (a prediction from a power model). Give it default arguments of a = 12 and b = 0.38. There should be no default value for weight. Use the default argument values (by passing only the value of weight to the function) to calculate height when weight = 42.

    3. The function in (2) assumes that the weight is provided in grams. Use the functions from (1) and (2) in combination to estimate the height for an animal that weighs 2 pounds using the default value for a, but changing the value for b to 0.32.

    Expected outputs for Writing Functions 2: 1
  4. Default Arguments (15 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.

    Rewrite your get_mass_from length() function from Use and Modify so that its arguments have default values 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. Only pass the function length, not a and b, so that the default values are used.
    Expected outputs for Default Arguments: 1
  5. Combining Functions (20 pts)

    This is a follow up to Default Argument.

    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 called convert_kg_to_pounds that converts kilograms into pounds (pounds = 2.205 * kg). Use that function and your get_mass_from_length() function from Default Arguments to estimate the weight, in pounds, of a 12 m long Stegosaurus with a = 10.95 and b = 2.64 (The estimated a and b values for Stegosauria from Seebacher 2001).

    Expected outputs for Combining Functions: 1
  6. Writing Tidyverse Functions (20 pts)

    1. Copy the following vectors into R and combine them into a data frame named count_data with columns named state, count, area, and site.

    state_vector <- c("FL", "FL", "FL", "FL", "GA", "GA", "GA", "GA", "SC", "SC", "SC", "SC")
    site_vector <- c("A", "B", "C", "D", "A", "B", "C", "D", "A", "B", "C", "D")
    count_vector <- c(9, 16, 3, 10, 2, 26, 5, 8, 17, 8, 2, 6)
    area_vector <- c(3, 5, 1.9, 2.7, 2, 2.6, 6.2, 4.5, 8, 4, 1, 3)
    

    2. Write a function takes takes two arguments: 1) a data frame with a count column and an area column; and 2) a column in that data frame to color the points by. Have the function make a plot with area on the x-axis and count on the y-axis and the points colored by the column you provided as an argument. Set the size of the points to 3. Use the function to make a scatter plot of count as a function of area for the count_data data frame with the points colored by the state column.

    3. Use the function from (2) to make a scatter plot of count as a function of area for the count_data data frame with the points colored by the site column.

    Expected outputs for Writing Tidyverse Functions: 1 2 3
  7. Portal Species Time-Series (optional)

    If surveys.csv, species.csv, and plots.csv are not available in your workspace download them:

    Load them into R using read.csv().

    First, combine the surveys and species tables into a single data frame.

    Then, write a function that:

    • Takes three arguments - a data frame (the combined table created in (1)), a genus name, and a species name
    • Uses dplyr to produce a data frame with a 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
    • Make a graph of the resulting time-series using ggplot2 that has year on the x axis, count on the y axis, and displays the data as blue points (with size = 2) connected by blue lines (with linewidth = 1). Change the x-axis label to Year and the y-axis label to Number of Individuals
    1. Use your function to plot the time-series for genus = "Dipodomys" and species = "merriami"
    2. Use your function to plot the time-series for genus = "Chaetodipus" and species = "penicillatus"
    Expected outputs for Portal Species Time-Series: 1 2

Assignment submission & checklist