Function template:

func_name <- function(arg1, arg2, ...) {
  
  func_code_here

  return(object_to_return)

}

In this template:

Remember that a function takes input (which could be multiple things), does something to that input, and then returns some kind of output.


Exercises

  1. This may be a type of function you are more familiar with. It is an equation that converts Celsius to Farenheit. A previous student of mine was basically Farenheit-illiterate; she never know what the weather is going to be like. Given this equation, can you write a function that converts a temperature value in Farenheit to Celsius for her?

Take your function for a spin, does it return the correct values?

2a. Given the following code chunk for reading buoy data files in for each year, describe the following:

buoy_1987 <- read_csv('./data/buoydata/44013_1987.csv', na = c("99", "999"))
buoy_1988 <- read_csv('./data/buoydata/44013_1988.csv', na = c("99", "999"))
buoy_1989 <- read_csv('./data/buoydata/44013_1989.csv', na = c("99", "999"))
buoy_1990 <- read_csv('./data/buoydata/44013_1990.csv', na = c("99", "999"))

2b. Use the str_c() function to write a function that creates the filename for each year. I’ve given you an example below if we were using str_c for just 1986. Consider this your starting point to build out a function.

str_c("./data/buoydata/44013_", 1986, ".csv", sep = "")
## [1] "./data/buoydata/44013_1986.csv"

Extra credit (2 points): Check out the glue package and do the same thing with glue().

2c. Complete the skeleton of this function based on the work that you have done up to now. Describe, in words, what is happening in every step.

read_buoy <- function(_________){
  
  filename <- ___________________________
  
  a_buoy <- read_csv(________________, ____________________)
  
  return(___________)

}

2d. Amend the read_buoy function to allow for a variable buoy number (currently we are using data from buoy 44013, but there are many other numbers/names that could be used!), directory location of the file, and year.

2e. Apply the workflow that you used in 2a - 2c to create a function to clean up the data using a dplyr workflow that will work for 1987, 2000, and 2007 Have it generate daily averaged wave heights and temperatures as well as renaming all of the columns to something understandable. Begin by writing a dplyr workflow for one data frame at a time. Then generalize it. Remember to ask yourself the following questions:

If you are not sure of some of these things, remember to run the code chunks bit by bit, putting in test values (e.g., one year of data) to ensure that you know what you are working with, what each line is doing, and what the final returned value is. Your answer might look similar to what we did in class, or, very different depending on how you write the function.


Modular Programming


3A-C. Using all that we previously created in the functions week and/or this homework, create a set of functions that, once a buoy is read in, returns a two facet ggplot2 object of a histogram of the difference between wind speed (WSPD) and gust speed (GST) and between the air temperature (ATMP) and water temperature (WTMP), so that you can later format and style it as you’d like. E.C.(+1 per question) Break the templates below into smaller modular functions.

gust_increase_hist <- function(a_year){
  #get the cleaned buoy data
  
  #create a long data frame with each row as a data point, measuring either 
  #difference between air and water OR wind speed and gust speed - one row per measurement
  #with a column that says WHAT that measurement is 
  
  #create a plot
  
}

buoy_measured_diff_long <- function(a_buoy){
  #with one buoy
  
  #calculate differences between ATMP and WTMP as well as WSPD and GST
  
  #pivot to make it long
  
  #return the modified data
}

plot_dual_hist <- function(summarized_buoy){
  #create a ggplot with a single variable as the x
  
  #make a histogram
  
  #facet by the measurement type
}

#test it out!

Final Project Prep


  1. Based on the data set you’re planning to use for your final, do you need to write any functions to clean the data as you bring it in? If so, describe it, and take a stab at writing it. If not, show us that the data loads cleanly.

  2. With the data you just loaded, make one visualization. But, before you do, articulate a question you want to answer with said visualization. What do you think you will see? Now make the plot. Did you see what you expected? What did the data tell you?