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 (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
Expected outputs for Writing Functions: 1prediction
that takes three arguments,x
,a
, andb
, and returnsy
usingy = a + b * x
(like a prediction from a simple linear model). Run it withx
= 12,a
= 6, andb
= 0.8.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
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 Theropoda that is 16 m long based on its reassembled skeleton.
- Create a new version of this function called
get_mass_from_length()
that 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.
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
, andb
, and returns an estimate ofheight
usingheight = a * weight ^ b
(a prediction from a power model). Give it default arguments ofa
= 12 andb
= 0.38. There should be no default value forweight
. Use the default argument values (by passing only the value ofweight
to the function) to calculateheight
whenweight
= 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
Expected outputs for Writing Functions 2: 1a
, but changing the value forb
to 0.32.Default Arguments (15 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 (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
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).Writing Tidyverse Functions (20 pts)
1. Copy the following vectors into R and combine them into a data frame named
count_data
with columns namedstate
,count
,area
, andsite
.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 anarea
column; and 2) a column in that data frame to color the points by. Have the function make a plot witharea
on the x-axis andcount
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 thecount_data
data frame with the points colored by thestate
column.3. Use the function from (2) to make a scatter plot of count as a function of area for the
Expected outputs for Writing Tidyverse Functions: 1 2 3count_data
data frame with the points colored by thesite
column.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
andspecies
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 aspecies
name - Uses
dplyr
to produce a data frame with a two columns:year
andcount
, wherecount
is the number of individuals (i.e., the number of rows) for the species indicated bygenus
andspecies
in thatyear
- Make a graph of the resulting time-series using
ggplot2
that hasyear
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 toYear
and the y-axis label toNumber of Individuals
- Use your function to plot the time-series for
genus
="Dipodomys"
andspecies
="merriami"
- Use your function to plot the time-series for
genus
="Chaetodipus"
andspecies
="penicillatus"
- Takes three arguments - a data frame (the combined table created in (1)), a