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
-
Topics
- Functions
-
Readings
Lecture Notes
Exercises
Writing Functions (25 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: 1Use 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 * length^b
Where the parameters
a
andb
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 as0.73
andb
has been estimated as3.63
; Seebacher 2001).get_mass_from_length_theropoda <- function(length){ mass <- 0.73 * length ^ 3.63 return(mass) }
- Use this function to print out the mass of a Spinosaurus that is 16 m long based on its reassembled skeleton.
- Create a new version of this function called
get_mass_from_length()
that estimates takeslength
,a
andb
as arguments and uses the following code to estimate the massmass <- a * length ^ b
. Use this function to estimate the mass of a Sauropoda (a = 214.44
,b = 1.46
) that is 26 m long.
Default Arguments (25 pts)
This is a follow up to Use and Modify.
Allowing
a
andb
to be passed as arguments toget_mass_from_length()
made the function more flexible, but for some types of dinosaurs we don’t have specific values ofa
andb
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 ofa = 39.9
andb = 2.6
(the average values from Seebacher 2001).- Use this function to estimate the mass of a Sauropoda (
a = 214.44
,b = 1.46
) that is 22 m long (by settinga
andb
when calling the function). - Use this function to estimate the mass of a dinosaur from an unknown taxonomic group that is 16m long.
Only pass the function
length
, nota
andb
, so that the default values are used.
- Use this function to estimate the mass of a Sauropoda (
Combining Functions (25 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
Expected outputs for Combining Functions: 1convert_kg_to_pounds
that converts kilograms into pounds (pounds = 2.205 * kg
). Use that function and yourget_mass_from_length()
function from Default Arguments to estimate the weight, in pounds, of a 12 m long Stegosaurus witha = 10.95
andb = 2.64
(The estimateda
andb
values for Stegosauria from Seebacher 2001).