---
title: "Writing Functions"
subtitle: "FOR 128: Lab 5"
author: "Insert Your Name Here"
date: "10/2/2024"
format:
  pdf: default
editor: 
  mode: source
---

## Welcome

Welcome to Lab 5! Today, we'll focus on writing our own custom functiuons. 

### Learning objectives

-   Get comfortable writing functions in `R`.
-   Specify multiple functionarguments and function argument defaults.
-   Create informative error messages with input checks. 
-   Use conditional statements within functions. 

### Deliverables (i.e., what to put in the lab drop box)

Upload your rendered PDF (`lab_05.pdf`) **and** Quarto (`lab_05.qmd`) document to the lab drop box. Make sure the Quarto document properly renders to PDF.

### Collaborator(s)

List any collaborators you worked with below. 

## Exercise 1

Write a function that takes three arguments: x, y, and z. This function should evaluate the power of x to the y to the z, i.e.
$$
x^{y^z}
$$

Call this function `pow_pow()`. Make sure to give an informative error if any of x, y, or z are not numeric. 


```{r}

```



## Exercise 2

In this course so far, we have focused on the arithmetic mean (i.e. the `mean()` in `R`). We can express the arithmetic mean mathematically as follows,
$$
\text{arithmetic mean} = \frac{1}{n}\sum_{i=1}^n x_i.
$$
However, in forestry we sometimes care about the *quadratic mean*. The quadratic mean is expressed as follows:
$$
\text{quadratic mean} = \frac{1}{n}\sqrt{\sum_{i=1}^n x_i^2}.
$$

See [Curtis and Marshall (2000)](https://www.fs.usda.gov/pnw/olympia/silv/publications/opt/436_CurtisMarshall2000.pdf) for more on the quadratic mean.

Write a function that takes a numeric vector as its input and outputs either the quadratic or arithmetic mean, based on a parameter specified by the user of your function. Call this function `general_mean()`. You function should allow the user to choose to remove NAs or not. Make sure to add a check in your function that the vector is numeric (and an informative error that if it is not), as well. 

```{r}

```

## Exercise 3 (BONUS)

This is an optional bonus exercise. Correct solutions will be given a small bonus on their lab 5 score (up to +5pts).

Write a function that sorts a numeric vector from least to greatest. Do not use the `sort()` function or any other `R` function used for sorting.  

```{r}

```

## Exercise 4

Do Exercise 5.1 from IFDAR

```{r}

```


## Exercise 5

Do Exercise 5.2 from IFDAR

```{r}

```


## Exercise 6

Do Exercise 5.3 from IFDAR

```{r}

```


## Exercise 7

Do Exercise 5.4 from IFDAR

```{r}

```


## Exercise 8

Do Exercise 5.5 from IFDAR

```{r}

```


## Exercise 9

Do Exercise 5.6 from IFDAR

```{r}

```


# Wrap up

Congratulations! You've made it to the end of Lab 5. Make sure to render your final document and submit both the .pdf and .qmd file to D2L.
