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


Lecture Notes

  1. Functions
  2. Conditionals


  1. -- 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 * Lengthb

    Where the parameters a and b 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 as 0.73 and b has been estimated as 3.63; Seebacher 2001).

    get_mass_from_length_theropoda <- function(length){
      mass <- 0.73 * length ** 3.63
    1. Add a comment to this function so that you know what it does.
    2. 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.
    3. 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 non-theropod species. Use this new function to estimate the mass of a Sauropoda (a = 214.44, b = 1.46) that is 26 m long.
    [click here for output]
  2. -- 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]
  3. -- 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, a has been estimated as 10.95 and b has been estimated as 2.64 (Seebacher 2001).

    [click here for output]
  4. -- 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 or FALSE.

    1. w is greater than 10
    2. w + x is less than 15
    3. x is greater than y
    4. 2 * x + 0.2 is equal to y
    5. dna1 is the same as dna2
    6. dna1 is not the same as dna2
    7. The number of occurrences of the base t is the same in dna1 and dna2

      Use str_count() from the stringr package. You’ll need to install this package before using it and will also want to familiarize yourself with str_length().

    8. w is greater than x, and y is greater than z
    9. x times w is between 13.2 and 13.5
    10. dna1 is longer than 5 bases, or z is less than w * x
    11. The combined length of dna1 and dna2 is greater than or equal to 30
    12. (w + x + y) divided by the logarithm (base 10) of 100 is equal to 7.15
    13. The GC content (which is always a percentage) of dna1 is not the same as the GC content of dna2
    [click here for output]
  5. -- 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 return FALSE. 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.

    1. 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 <- _________
    2. Improve the documentation for the function so that it is clear what near means and what output the user should expect.
    3. Check if Point 1 (latitude = 29.65, longitude = -82.33) is near Point 2 (latitude = 41.74, longitude = -111.83).
    4. Check if Point 1 (latitude = 29.65, longitude = -82.33) is near Point 2 (latitude = 30.5, longitude = -82.8).
    5. 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.
    6. Improve the documentation for the new function so that it reflects this new behavior
    7. 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.
    [click here for output]
  6. -- Function with Choices --

    Write a function that concatenates and prints:

    The ultimate answer to the ultimate question of life, the universe, and everything is: XXX.

    Where XXX is either a string or a number that is passed to the function as a parameter. Use this function to print out the answer if the input parameter is 42, but don’t actually do the printing from inside the function (think about why printing from outside the function might generally be more useful).

    If you don’t understand why this question is fun/funny you can Google it or, better yet, actually read Hitchhiker’s Guide to the Galaxy, which is one of the funniest books ever written. There is also a very valuable programming lesson to this story.

    [click here for output]

Check out the solution