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. As someone who is Farenheit-illiterate, I 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?

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('./buoydata/44013_1987.csv', na.strings = c("99", "999"))
buoy_1988 <- read.csv('./buoydata/44013_1988.csv', na.strings = c("99", "999"))
buoy_1989 <- read.csv('./buoydata/44013_1989.csv', na.strings = c("99", "999"))
buoy_1990 <- read.csv('./buoydata/44013_1990.csv', na.strings = c("99", "999"))

2b. Use the paste() or paste0() function to write the the filename for each year. What is the output of the paste()/paste0() function? I’ve given you an example below.

paste('./buoydata/44013_', 1986, '.csv', sep = '')
## [1] "./buoydata/44013_1986.csv"
# alternatively
paste0('./buoydata/44013_', 1986, '.csv')
## [1] "./buoydata/44013_1986.csv"

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 process_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!) and year.

2e. Apply the workflow that you used in 2a - 2c to turn the data clean up dplyr flow that we came up with. 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.

#Now the data transformations
buoy_1986 <- buoy_1986 %>%
  select(YY, MM, DD, WVHT, WTMP) %>%
  rename(Year = YY,
         Month = MM,
         Day = DD,
         Wave_Height = WVHT,
         Temperature_c = WTMP) %>%
  group_by(Year, Month, Day) %>%
  summarise(Wave_Height = mean(Wave_Height, na.rm=T),
            Temperature_c = mean(Temperature_c, na.rm=T)) %>%
    ungroup()

#1987
buoy_1987 <- read.csv("./buoydata/44013_1987.csv", na.strings="99")

#Now the data transformations
buoy_1987 <- buoy_1987 %>%
  select(YY, MM, DD, WVHT, WTMP) %>%
  rename(Year = YY,
         Month = MM,
         Day = DD,
         Wave_Height = WVHT,
         Temperature_c = WTMP) %>%
  group_by(Year, Month, Day) %>%
  summarise(Wave_Height = mean(Wave_Height, na.rm=T),
            Temperature_c = mean(Temperature_c, na.rm=T)) %>%
    ungroup()

buoydata <- rbind(buoy_1986, buoy_1987)

Using conditionals!


Logical Operators

See - http://www.statmethods.net/management/operators.html

Operator Description
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== exactly equal to
!= not equal to
!x Not x
x y
x & y x AND y
isTRUE(x) test if X is TRUE

If statement structure:

See http://www.programiz.com/r-programming/if-else-statement for a good tutorial on if/else statements.

if (test_expression) {
   statement
}

If … else … statement structure:

if (test_expression) {
   statement1
} else {
   statement2
}

Let’s introduce condition statements/tests. These are if, then else, statements.

x <- 4 == 3
x
## [1] FALSE
### If statement
x <- 5
if (x > 0) {
   print("Positive number")
} 
## [1] "Positive number"
### If/else statement
x <- -5
if(x > 0){
   print("Non-negative number")
} else {
   print("Negative number")
}
## [1] "Negative number"
### But that doesn't account for zero!
x <- 0
if(x > 0){
   print("Non-negative number")
} else {
   print("Negative number")
}
## [1] "Negative number"
x <- 0
if (x > 0) {
   print("Non-negative number")
} else {
   print("Negative number")
}
## [1] "Negative number"
  1. I come from ‘tropical Canada’. I don’t like the cold but I really don’t like it when it’s hot. Although I want to know what the temperature is in Celsius when the US weather channel reports it to me in Farenheit, there are certain points at which it is just too cold or too hot for me to care about the exact value. Modify the f_to_c function below to print the following:
f_to_c <- function(farenheit) {
  celsius <- (farenheit -  32)  *  5 / 9 
  return(celsius)
}