Learning Objectives
Following this assignment students should be able to:
- understand and use the basic relational operators
- use an
if
statement to evaluate conditionals- use
if
statements with functions
Reading
-
Topics
- Conditionals
-
Readings
Lecture Notes
Exercises
Choice Operators (20 pts)
Create the following variables.
w <- 10.2 x <- 1.3 y <- 2.8 z <- 17.5 colors <- c("red", "blue", "green") masses <- c(45.2, 36.1, 27.8, 81.6, 42.4) dna1 <- "attattaggaccaca" dna2 <- "attattaggaacaca"
Use them to print whether or not the following statements are
TRUE
orFALSE
.w
is greater than 10"green"
is incolors
x
is greater thany
- Each value in
masses
is greater than 40. - 2 *
x
+ 0.2 is equal toy
dna1
is the same asdna2
dna1
is not the same asdna2
w
is greater thanx
, ory
is greater thanz
x
timesw
is between 13.2 and 13.5- Each mass in
masses
is between 30 and 50.
Basic If Statements (20 pts)
1. Complete (i.e., copy into your code and them modify) the following
if
statement so that ifage_class
is equal to “sapling” it setsy <- 10
.age_class = "sapling" if (){ } y
2. Complete the following
if
statement so that ifage_class
is equal to “sapling” it setsy <- 10
and ifage_class
is equal to “seedling” it setsy <- 5
.age_class = "seedling" if (){ } y
3. Complete the following
if
statement so that ifage_class
is equal to “sapling” it setsy <- 10
and ifage_class
is equal to “seedling” it setsy <- 5
and ifage_class
is something else then it sets the value ofy <- 0
.age_class = "adult" if (){ } y
4. Convert your conditional statement from (3) into a function that takes
[click here for output]age_class
as an argument and returnsy
. Call this function 5 times, once with each of the following values forage_class
: “sapling”, “seedling”, “adult”, “mature”, “established”.Load or Download File (20 pts)
With large data files it can be useful to only download the file if it hasn’t already been downloaded. One way to do this is to check if the file name exists in your working directory. If it does then load it, if not then download it. You can use the
list.files()
function to get a lit of files and directories in the working directory and thedownload.file(url, filename)
function to download the file at aurl
to a specificfilename
.-
Write a conditional statement that checks if
surveys.csv
exists in the working directory, downloads it from https://ndownloader.figshare.com/files/2292172 using if it is not, loads the file into a data frame and displays the first few rows using thehead()
function. -
Make a version of this conditional statement that is a function, where the name of the file is the first argument and the link for downloading the file is the second argument. The function should return the resulting data frame. Add some documentation to the top of the function describing what it does. Call this function using “species.csv” as the file name and https://ndownloader.figshare.com/files/3299483 as the link. Print the first few rows of the resulting data frame using
head()
.
-
Size Estimates by Name (20 pts)
This is a follow up to Use and Modify.
To make it even easier to work with your dinosaur size estimation functions you decide to create a function that lets you specify which dinosaur group you need to estimate the size of by name and then have the function automatically choose the right parameters.
Create a new function
get_mass_from_length_by_name()
that takes two arguments, thelength
and the name of the dinosaur group. Inside this function useif
/else if
/else
statements to check to see if the name is one of the following values and if so seta
andb
to the appropriate values.- Stegosauria:
a
=10.95
andb
=2.64
(Seebacher 2001). - Theropoda:
a
=0.73
andb
=3.63
(Seebacher 2001). - Sauropoda:
a
=214.44
andb
=1.46
(Seebacher 2001).
If the name is not any of these values set
a
andb
toNA
.Once the function has assigned
a
andb
have it runget_mass_from_length()
with the appropriate values and return the estimated mass.Run the function for:
- A Stegosauria that is 10 meters long.
- A Theropoda that is 8 meters long.
- A Sauropoda that is 12 meters long.
- A Ankylosauria that is 13 meters long.
Challenge (optional): If the name is not one of values that have
a
andb
values print out a message that it doesn’t know how to convert that group that includes that groups name in a message like “No known estimation for Ankylosauria”. (the functionpaste
will be helpful here). Doing this successfully will modify your answer to (4).Challenge (optional): Change your function so that it uses two different values of
[click here for output]a
andb
for Stegosauria. When Stegosauria is greater than 8 meters long use the equation above. When it is less than 8 meters long usea
=8.5
andb
=2.8
. Run the function for a Stegosauria that is 6 meters long.- Stegosauria:
DNA or RNA (20 pts)
Write a function that determines if a sequence of base pairs is DNA, RNA, or if it is not possible to tell given the sequence provided. RNA has the base Uracil (
"u"
) instead of the base Thymine ("t"
), so sequences with u’s are RNA, sequences with t’s are DNA, and sequences with neither are unknown.You can check if a string contains a character (or a longer substring) in R using
grepl(substring, string)
, sogrepl("u", sequence)
will check if the string in thesequence
variable has the baseu
.Name the function
dna_or_rna()
and have it takesequence
as an argument. Have the function return one of three outputs:"DNA"
,"RNA"
, or"UNKNOWN"
. Add documentation describing what the function does. Call the function on each of the following sequences.seq1 <- "ttgaatgccttacaactgatcattacacaggcggcatgaagcaaaaatatactgtgaaccaatgcaggcg" seq2 <- "gauuauuccccacaaagggagugggauuaggagcugcaucauuuacaagagcagaauguuucaaaugcau" seq3 <- "gaaagcaagaaaaggcaggcgaggaagggaagaagggggggaaacc"
Challenge (optional): Figure out how to make your function work with both upper and lower case letters, or even strings with mixed capitalization*
[click here for output]Unit Conversion Challenge (optional)
Measures of the amount of energy used by biological processes are critical to understanding many aspects of biology from cellular physiology to ecosystem ecology. There are many different units for energy use and their utilization varies across methods, research areas, and lab groups. Write a function,
convert_energy_units(energy_value, input_unit, output_unit)
to convert units between the following energy values - Joules(J), Kilojoules(KJ), Calories(CAL), and Kilocalories (KCAL; this is unit used for labeling the amount of energy contained in food). A Kilojoule is 1000 Joules, a Calorie is 4.1868 Joules, a Kilocalorie is 4186.8 Joules. An example of a call to this function would look like:energy_in_cal <- 200 energy_in_j <- convert_energy_units(energy_in_cal, "CAL", "J")
Make this function more efficient by linking
if else
statements. If either the input unit or the output unit do not match the five types given above, have the function print - “Sorry, I don’t know how to convert “ + the name of the unit provided. Instead of writing an individual conversion between each of the different currencies (which would require 12 if statements) you could choose to convert all of the input units to a common scale and then convert from that common scale to the output units. This approach is especially useful since we might need to add new units later and this will be much easier using this approach.Use your function to answer the following questions:
- What is the daily metabolic energy used by a human (~2500 KCALs) in Joules.
- How many times more energy does a common seal use than a human? The common seal uses ~52,500 KJ/day (Nagy et al. 1999). Use the daily human metabolic cost given above.
- How many ergs (ERG) are there in one kilocalorie. Since we didn’t include the erg conversion this should trigger our ‘don’t know how to convert’ message