Day 1: Functions in R

Learning Objectives

This week, students will:

  • learn the parts of a function

Practice Objectives

  • Reading and writing tables
  • Reading and plotting phylogenies

Setup your RStudio project (5 min)

Writing functions (10 min)

https://spaceplace.nasa.gov/planets-weight/en/

Pounds to kilograms Kilograms to pounds

Exercise 1: Converting between units of mass


Using and modifying functions (20 min)

mass = a * length^b

Exercise 2: The weight of dinosaurs


Why do we use functions in programming? (15 min)

Discussion in small groups (5 min)

Discussion in full (10 min)


Defining argument values in a function (20 min)

Exercise 3: The general form of a formula


Combining functions (20 min)

Exercise 4: Facilitating impactful science

Homework:

Instructions

  • Open a new file called β€œfunctions-after.Rmd” that is saved in your β€œdocuments” folder.
  • Write the code to solve the following exercises in R chunks.
  • Add comments to each line of code explaining with your own words what the code is doing.
  • Once you are finished, knit to PDF.
  • Git add, commit and push the new files (PDF and Rmd) to your remote repository.

Exercise 5: Creating a function for the Portal data set



Day 2: Making choices inside R functions

Review from last class (10 min)

Setup your RStudio project (5 min)

Review: Logical and conditional statements (15 min)

Exercise 6: Practice logical and conditional statements


Basic if statement (10 min)

Exercise 7: Handling one choice


The if/else statement (10 min)

Exercise 8: Handling 2 choices


The if/else if/else statement (10 min)

Exercise 9: Handling 3 choices or more


Using conditions inside functions (30 min)

Exercise 10: Value of y by age class (10 min)

Exercise 11: Mass estimates by biological group (20 min)


Homework:

Instructions

  • Open a new file called β€œchoices-after.Rmd” that is saved in your β€œdocuments” folder.
  • Write the code to solve the following exercises in R chunks.
  • Add comments to each line of code explaining with your own words what the code is doing.
  • Once you are finished, knit to PDF.
  • Git add, commit and push the new files (PDF and Rmd) to your remote repository.

Exercise 12: Load or download a file?

Exercise 13: A function for the UHURU data set

Exercise 14: Energy conversion challenge