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

Lecture Notes

  1. Conditionals
  2. Floating Point
  3. Style

Exercises

  1. 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 or FALSE.

    1. w is greater than 10
    2. "green" is in colors
    3. x is greater than y
    4. Each value in masses is greater than 40.
    5. 2 * x + 0.2 is equal to y
    6. dna1 is the same as dna2
    7. dna1 is not the same as dna2
    8. w is greater than x, or y is greater than z
    9. x times w is between 13.2 and 13.5
    10. Each mass in masses is between 30 and 50.
    [click here for output]
  2. Basic If Statements (20 pts)

    1. Complete (i.e., copy into your code and them modify) the following if statement so that if age_class is equal to “sapling” it sets y <- 10.

    age_class = "sapling"
    if (){
      
    }
    y
    

    2. Complete the following if statement so that if age_class is equal to “sapling” it sets y <- 10 and if age_class is equal to “seedling” it sets y <- 5.

    age_class = "seedling"
    if (){
      
    }
    y
    

    3. Complete the following if statement so that if age_class is equal to “sapling” it sets y <- 10 and if age_class is equal to “seedling” it sets y <- 5 and if age_class is something else then it sets the value of y <- 0.

    age_class = "adult"
    if (){
      
    }
    y
    

    4. Convert your conditional statement from (3) into a function that takes age_class as an argument and returns y. Call this function 5 times, once with each of the following values for age_class: “sapling”, “seedling”, “adult”, “mature”, “established”.

    [click here for output]
  3. 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 the download.file(url, filename) function to download the file at a url to a specific filename.

    1. 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 the head() function.

    2. 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().

    [click here for output]
  4. 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, the length and the name of the dinosaur group. Inside this function use if/else if/else statements to check to see if the name is one of the following values and if so set a and b to the appropriate values.

    If the name is not any of these values set a and b to NA.

    Once the function has assigned a and b have it run get_mass_from_length() with the appropriate values and return the estimated mass.

    Run the function for:

    1. A Stegosauria that is 10 meters long.
    2. A Theropoda that is 8 meters long.
    3. A Sauropoda that is 12 meters long.
    4. A Ankylosauria that is 13 meters long.

    Challenge (optional): If the name is not one of values that have a and b 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 function paste 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 a and b for Stegosauria. When Stegosauria is greater than 8 meters long use the equation above. When it is less than 8 meters long use a = 8.5 and b = 2.8. Run the function for a Stegosauria that is 6 meters long.

    [click here for output]
  5. 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), so grepl("u", sequence) will check if the string in the sequence variable has the base u.

    Name the function dna_or_rna() and have it take sequence 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]
  6. 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:

    1. What is the daily metabolic energy used by a human (~2500 KCALs) in Joules.
    2. 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.
    3. 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
    [click here for output]

Assignment submission & checklist