### 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

• Topics

• Functions

### Exercises

1. #### Writing Functions (15 pts)

Copy the following function into your assignment and replace the `________` with variables 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.

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 Spinosaurus 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. #### 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
4. #### Combining Functions (15 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
5. #### Tree Biomass Challenge (40 pts)

Understanding the total amount of biomass (the total mass of all individuals) in forests is important for understanding the global carbon budget and how the earth will respond to increases in carbon dioxide emissions.

We donâ€™t normally measure the mass of a tree, but take a measurement of the diameter or circumference of the trunk and then estimate mass using equations like M = 0.124 * D2.53.

1. Estimate tree biomass for each species in a 96 hectare area of the Western Ghats in India using the following steps.

• If the file `ramesh2010-macroplots.csv` isnâ€™t already in your workspace then download a copy.
• Load the data into R.
• Write a function that takes a vector of tree diameters as an argument and returns a vector of tree masses.
• Create a `dplyr` pipeline that
• Adds a new column (using `mutate` and your function) that contains masses calculated from the diameters
• Groups the data frame into species using the `SpCode` column
• And then calculates biomass (i.e., the `sum` of the masses) for each species (using `summarize`)
• Stores the result as a data frame
• Display the resulting data frame

2. Plot a histogram of the species biomass values you just calculated.

• Use 10 bins in the histogram (using the `bins` argument)
• Use a log10 scale for the x axis (using `scale_x_log10`)
• Change the x axis label to `Biomass` and the y axis label to `Number of Species` (using `labs`)
Expected outputs for Tree Biomass Challenge: 1 2
6. #### Portal Species Time-Series (optional)

Load them into R using `read.csv()`.

Do the following (no output is expected for (1) or (2)):

1. Combine the `surveys` and `species` tables into a single data frame
2. 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`
• Returns the resulting data frame
3. Use your function to get the time-series for `genus` = `"Dipodomys"` and `species` = `"merriami"` and then make a graph of this time-series using `ggplot` that has `year` on the x axis, `count` on the y axis, and displays the data as points.
4. Use your function to get the time-series for `genus` = `"Chaetodipus"` and `species` = `"penicillatus"` and then make a graph of this time-series using `ggplot` that has `year` 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 to `Year` and the y axis label to `Number of Individuals`
Expected outputs for Portal Species Time-Series: 1 2

Assignment submission & checklist