Part 0. Setup

  • Create a repo on GitHub named eds221-day4-activities
  • Clone to create a version-controlled R Project
  • Create some subfolder infrastructure (docs, data)
  • Add a new Quarto file, save as loops_and_functions.qmd in docs

Part 1. For loops revisited

Task 1

Create two sequences, one called weekdays that contains days of the week (“Monday”, “Tuesday”, “Wednesday”, etc.) and one called transects that contains the series of transect names “Transect A”, “Transect B,”Transect C”. Write a nested for loop that creates a matrix containing the following:

Monday - Transect A Monday - Transect B Monday - Transect C
Tuesday - Transect A Tuesday - Transect B Tuesday - Transect C
Wednesday - Transect A Wednesday - Transect B Wednesday - Transect C
Thursday - Transect A Thursday - Transect B Thursday - Transect C
Friday - Transect A Friday - Transect B Friday - Transect C
Saturday - Transect A Saturday - Transect B Saturday - Transect C
Sunday - Transect A Sunday - Transect B Sunday - Transect C

Part 2. Functions introduced

Task 2

Write a function called force that calculates a force (in Newtons), given inputs of mass (in kg) and acceleration (in \(\frac{m}{s^2}\) (recall: \(F = ma\)), and returns a statement “The resulting force is ___ Newtons.”

Task 3

The length:weight relationship for fish is: \(W=aL^b\), where where L is total fish length (centimeters), W is the expected fish weight (grams), and a and b are species-dependent parameter values (shown below for several fish from Peyton et al. 2016).

sci_name common_name a_est b_est
Chanos chanos Milkfish 0.0905 2.52
Sphyraena barracuda Great barracuda 0.0181 3.27
Caranx ignobilis Giant trevally 0.0353 3.05

Recreate the table above as a data frame stored as fish_parms. Then, write a function called fish_weight that allows a user to only enter the common name (argument fish_name) and total length (argument tot_length) (in centimeters) of a fish, to return the expected fish weight in grams. Test it out for different species and lengths.

Now, try creating a vector of lengths (e.g. 0 to 100, by increments of 1) and ensuring that your function will calculate the fish weight over a range of lengths for the given species (try this for milkfish, storing the output weights as milkfish_weights.

Task 4

Wave power (more accurately wave energy flux) in deep water is approximated by:

\[P_{deep}=0.5 H^2 T\] where \(P\) is power in \(\frac{kW}{m}\) (potential power per wave meter), \(H\) is wave height in meters (more specifically, the significant wave height), and \(T\) is the wave period in seconds. Learn more here.

Write a function called wave_power that calculates potential ocean wave power given inputs of wave height and period.

Use your wave_power function to approximate wave power for a period of 8 seconds, over a range of wave heights from 0 to 3 meters by increments of 0.2 meters.

Task 5 (OPTIONAL)

The wave energy equation changes based on ocean depth. Along the coast of Brenville, which has a very sharp shelf as the wave approaches the coast, wave energy is approximated using the deep ocean equation (the one you used above) for depths > 12 meters, and a shallow equation for depths <= 12 meters. The Brenville team estimates shallow wave power by:

\[P_{shallow}=0.81 H^2 T\]

Create a function that requires inputs of water depth, wave height and period, then returns the approximated wave power using the correct equation for the depth entered. It should also include a message (hint: use message() just like you would use warning!) that lets the user know if the shallow or deep water equation was used.

Citations

Peyton, K. A., T. S. Sakihara, L. K. Nishiura, T. T. Shindo, T. E. Shimoda, S. Hau, A. Akiona, and K. Lorance. 2016. “Length–Weight Relationships for Common Juvenile Fishes and Prey Species in Hawaiian Estuaries.” Journal of Applied Ichthyology 32 (3): 499–502. https://doi.org/10.1111/jai.12957.