Functions

Practical Computing and Data Science Tools

Annoucements

  • Midterm 1 is in one week, Oct 3, during lab time.
  • Lab 3 grades coming soon.
  • Lab 4 grades by end of weekend.

Agenda

  • Warm-up
  • Loops
  • Forestry functions
  • Quarto practice

Warm-up

Warm-up activity

Write a function that takes two vectors as arguments (inputs) and does the following:

Takes the difference of the vectors’ means. In other words, take the mean() of each input and subtract the first input’s mean() from the second input’s mean(). Make sure to handle NA values that might exist in the vectors. Name this function diff_in_means().

Bonus: (only do if you have time) add a requirement that a and b are the same length vector.

05:00

Warm-up activity: possible solution

diff_in_means <- function(a, b, na.rm = FALSE) {
  if (na.rm) {
    a <- a[!is.na(a)]
    b <- b[!is.na[b]]
  }
  
  a_mean <- mean(a)
  b_mean <- mean(b)
  
  return(a_mean - b_mean)
}

Warm-up activity: possible solution with bonus

diff_in_means <- function(a, b, na.rm = FALSE) {
  stopifnot(
    "Arguments `a` and `b` must be the same length." = length(a) == length(b)
  )
  
  if (na.rm) {
    a <- a[!is.na(a)]
    b <- b[!is.na(b)]
  }
  
  a_mean <- mean(a)
  b_mean <- mean(b)
  
  return(a_mean - b_mean)
}

Loops

Loops

  • Loops are important to R and many other programming languages.
  • Understanding how to read, write, and utilize loops are key skills for data scientists.
  • We’ll introduce two types of loops in R: while and for loops

while loops

  • while loops look as follows:
while (condition is true) {
  run this code until condition is false
}

while loops

for loops

  • for loops look as follows:
for (variable in vector) {
  run this code until through each element of the vector
}

for loops

for loops

for loop in a function

my_mean <- function(x) {
  total <- 0
  
  for (i in 1:length(x)) {
    total <- total + x[i]
  }
  
  average <- total / length(x)
  
  return(average)
}

Functions for forestry

Basal area

  • Basal area is the measurement of a tree’s cross-sectional area at breast height, and is derived from the diameter at breast height measurement.
  • Oftentimes, we care about the basal area in a given stand and want to report basal area on a per-unit-area basis (e.g. square feet per acre or square meters per hectare), but we only have DBH measurements.

Calculating basal area

Basal area in square feet per acre: \[ \text{BA} = \frac{\pi}{144} \cdot \left( \frac{\text{DBH}}{2} \right)^2 = \frac{\pi}{4\cdot 144} \cdot \text{DBH}^2 \] Basal area in square meters per hectare: \[ \text{BA} = \frac{\pi}{10000} \cdot \left( \frac{\text{DBH}}{2} \right)^2 = \frac{\pi}{4\cdot 10000} \cdot \text{DBH}^2 \] Basal area is of the form:

\(\text{forester's constant} \times \text{DBH}^2\)

Implementation in R

What we want: A function that

  • has sensible input checks,
  • computes basal area in either square feet per acre or square meters per hectare, and
  • is flexible enough to allow for DBH measurements to be in inches or centimeters.

Implementation in R

How we’ll do it:

  • Define the forester’s constant for whichever units DBH measurements are in,
  • Convert inches to centimeters or vise versa to satisfy our formula for basal area,
  • Compute basal area based on our formula,
  • Go back and add some sensible input checks.

Calculating basal area

Basal area in square feet per acre: \[ \text{BA} = \frac{\pi}{144} \cdot \left( \frac{\text{DBH}}{2} \right)^2 = \frac{\pi}{4\cdot 144} \cdot \text{DBH}^2 \] Basal area in square meters per hectare: \[ \text{BA} = \frac{\pi}{10000} \cdot \left( \frac{\text{DBH}}{2} \right)^2 = \frac{\pi}{4\cdot 10000} \cdot \text{DBH}^2 \] There are 2.54 centimeters in an inch.

Next time

  • Introduction to the tidyverse

See you all upstairs in lab!