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 (10 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
Expected outputs for Basic If Statementsif
/else if
/else
statement from (3) into a function that takesage_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”.If Statements In Functions (20 pts)
- Write a function named
double_if_small
that takes a number as input and returns the number multiplied by 2 if the input is less than 26 and returns just the number (not multiplied by two) if the input is greater than or equal to 26. Call the function with10
as the input. - Call the function from (1) with
30
as the input. - Write a function called
prediction
that takes a single argumentx
. Ifx
is both greater than 0 and less than 15 then returny = 6 + 0.8 * x
. Ifx
is both greater than 15 and less than 30 then returny = 5 + 0.75 * x
. In all other cases returny = NA
. Call the function with5
as the input. - Call the function from (3) with
26
as the input. - Call the function from (3) with
-2
as the input.
- Write a function named
Size Estimates by Name (20 pts)
You’re going to write a function to estimate a dinosaur’s mass based on its length. The general form of the equation for doing this is:
mass <- a * length ^ b
The parameters
a
andb
vary by the group of dinosaurs, so 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 use the associateda
andb
values to estimate the species mass using these equations:- Stegosauria:
mass = 10.95 * length ^ 2.64
(Seebacher 2001) - Theropoda:
mass = 0.73 * length ^ 3.63
(Seebacher 2001) - Sauropoda:
mass = 214.44 * length ^ 1.46
(Seebacher 2001)
If the name is not any of these values the function should return
NA
.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 warning that it doesn’t know how to convert that group that includes that groups name in a message like “No known estimation for Ankylosauria”. You can use the functionwarning("your warning text")
to print a warning and the functionpaste()
to combine text with a value from a variablepaste("My name is", name)
. Doing this successfully will modify your answer to (4), which is fine.Challenge (optional): Change your function so that it uses two different values of
Expected outputs for Size Estimates by Namea
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 the
str_detect
function from thestringr
package:str_detect(string, substring)
, which will returnTRUE
ifsubstring
is present instring
. Sostr_detect(sequence, "u")
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"
. 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.
Expected outputs for DNA or RNACheck That Your Code Runs (10 pts)
Sometimes you think you’re code runs, but it only actually works because of something else you did previously. To make sure it actually runs you should save your work and then run it in a clean environment.
Follow these steps in RStudio to make sure your code really runs:
1. Restart R (see above) by clicking
Session
in the menu bar and selectingRestart R
:2. If the
Environment
tab isn’t empty click on the broom icon to clear it:The
Environment
tab should now say “Environment Is Empty”:3. Rerun your entire homework assignment using “Source with Echo” to make sure it runs from start to finish and produces the expected results.
Expected outputs for Check That Your Code RunsLoad or Download File (Challenge - optional)
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 list 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, if it doesn’t then downloads it from https://ndownloader.figshare.com/files/2292172 usingdownload.file()
, and finally loads the file into a data frame and displays the first few rows using thehead()
function. The url needs to be in quotes since it is character data. -
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()
.
-
Unit Conversion Challenge (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 returnNA
and display a warning (using thewarning()
function) - “Cannot 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 ‘Cannot convert’ message