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)

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

Discussion in small groups (5 min)

Discussion in full (10 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


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 1: 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 1: Practice logical and conditional statements


Basic if statement (10 min)

Exercise 2: Handling one choice


The if/else statement (10 min)

Exercise 3: Handling 2 choices


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

Exercise 4: Handling 3 choices or more


Using conditions inside functions (30 min)

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

Exercise 6: 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 1: Load or download a file?

Exercise 2: A function for the UHURU data set

Exercise 3: Energy conversion challenge