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
 understand and use the basic relational operators
 use an
if
statement to evaluate conditionals
Reading

Topics
 Functions
 Conditionals

Readings
Lecture Notes
Exercises
 Use and Modify 
The length of an organism is typically strongly correlated with it’s 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 it’s 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) }
 Add a comment to this function so that you know what it does.
 Use this function to print out the mass of a Spinosaurus that is 16 m long based on it’s reassembled skeleton. Spinosaurus is a predator that is bigger, and therefore, by definition, cooler, than that stupid Tyrannosaurus that everyone likes so much.
 Create a new version of this function called
get_mass_from_length()
that estimates the mass of an organism in kg based on it’s length in meters by taking length, a, and b as parameters. To be clear we want to pass the function all 3 values that it needs to estimate a mass as parameters. This makes it much easier to reuse for all of the nontheropod species. Use this new function to estimate the mass of a Sauropoda (a = 214.44
,b = 1.46
) that is 26 m long.
 Writing Functions 
Write a function that converts pounds to grams (there are 453.592 grams in one pound). Use that function and a built in function to print out how many grams there are in 3.75 pounds, rounded to the nearest gram.
Don’t do any printing or rounding inside your function. You want each function to do one thing and do it well, and in this case that thing is converting pounds to grams. Have the function do the conversion and then do the rounding and printing outside of the function.
[click here for output] Nested Functions 
This is a follow up to Writing Functions.
Measuring things using the metric system is great for us scientists, but when you call your grandmother this weekend (you do call your grandmother every weekend don’t you?) you’d like to tell her that you’re taking this awesome class where you learned how to program a computer to calculate how much dinosaurs weigh (if you phrase it like this she’ll have something really cool to brag about to her friends), and that you calculated the weight of a Stegosaurus (no need to scare grandma by talking about the totally awesome, and really scary, Spinosaurus). The problem is that your grandmother doesn’t really know what a kilogram is, she wants to know what it weighed in pounds. So, hook your grandmother up and write a function that converts kilograms into pounds (there are 2.205 pounds in a kilogram). Use that function along with your dinosaur mass function to estimate the weight, in pounds, of a 12 m long Stegosaurus (12 m is about as big as they come and we want grandma’s story to be as wild as possible). In Stegosauria,
[click here for output]a
has been estimated as10.95
andb
has been estimated as2.64
(Seebacher 2001). Choice Operators 
Create the following variables.
w < 10.2 x < 1.3 y < 2.8 z < 17.5 dna1 < "attattaggaccaca" dna2 < "attattaggaacaca"
Use them to print whether or not the following statements are
TRUE
orFALSE
.w
is greater than 10w
+x
is less than 15x
is greater thany
 2 *
x
+ 0.2 is equal toy
dna1
is the same asdna2
dna1
is not the same asdna2

The number of occurrences of the base t is the same in
dna1
anddna2
Use
str_count()
from thestringr
package. You’ll need to install this package before using it and will also want to familiarize yourself withstr_length()
. w
is greater thanx
, andy
is greater thanz
x
timesw
is between 13.2 and 13.5dna1
is longer than 5 bases, orz
is less thanw
*x
 The combined length of
dna1
anddna2
is greater than or equal to 30  (
w
+x
+y
) divided by the logarithm (base 10) of 100 is equal to 7.15  The GC content (which is always a percentage) of
dna1
is not the same as the GC content ofdna2
 Complete the Code 
The following function is intended to check if two geographic points are close to one another. If they are it should return
TRUE
. If they aren’t, it should returnFALSE
. Two points are considered near to each other if the absolute value of the difference in their latitudes is less than one and the absolute value of the difference in their longitudes is less than one.
Fill in the
_________
in the function to make it work.near < function(lat1, long1, lat2, long2){ # Check if two geographic points are near each other if ((abs(lat1  lat2) < 1) & (_________){ near < TRUE } else { near < _________ } return(near) }
 Improve the documentation for the function so that it is clear what near means and what output the user should expect.
 Check if Point 1 (latitude = 29.65, longitude = 82.33) is near Point 2 (latitude = 41.74, longitude = 111.83).
 Check if Point 1 (latitude = 29.65, longitude = 82.33) is near Point 2 (latitude = 30.5, longitude = 82.8).
 Create a new version of the function that improves it by allowing the user to pass in a parameter that sets what “near” means. To avoid changing the existing behavior of the function (since some of your lab mates are using it already) give the parameter a default value of 1.
 Improve the documentation for the new function so that it reflects this new behavior
 Check if Point 1 (latitude = 48.86, longitude = 2.35) is near Point 2 (latitude = 41.89, longitude = 2.5), when near is set to 7.

 Choices with Functions 
The UHURU experiment in Kenya has conducted a survey of Acacia drepanolobium among each of their ungulate exclosure treatments. Data for the survey is available here in a tab delimited (
"\t"
) format. Each of the individuals surveyed were measured for branch circumference (CIRC
) and canopy width (AXIS1
) and was identified for the associated antsymbiont species present (ANT
).The following function takes a subset of the data for a given
ANT
symbiont and evaluates the linear regression (lm()
) for a given relationship, returning the symbiontspecies
used for the subset and ther2
of the model.report_rsquared < function(data, species, formula){ subset < dplyr::filter(data, ANT == species) test < lm(formula, data = subset) rsquared < round(summary(test)$r.squared, 3) output < data.frame(species = species, r2 = rsquared) return(output) }
 Execute the function using the UHURU data
and specifying
species = "CM"
andformula = AXIS1~CIRC
.  Modify the function so that it also determines
if()
thersquared
is significant based on a giventhreshold
. The modified function shouldreturn()
thespecies
,rsquared
and asignificance
value of"S"
for a relationship with anrsquared > threshold
or"NS"
for anrsquared < threshold
.  Execute your modified function for
species
of"CM"
,"CS"
, and"TP"
given athreshold = 0.667
.
 Execute the function using the UHURU data
and specifying