Function template:
func_name <- function(arg1, arg2, ...) {
func_code_here
return(object_to_return)
}
In this template:
function_name is what you decide to call your function. This is usually a verb that describes what the function does; e.g., ‘get_max_diff’, ‘get_first_year’, …
arg1 this is the name of an argument (again you decide what the name is). This is what you will call the input when you are within the body of the function code
function_code_here is where you write the code. This is where you transform your inputs into the output
Remember that a function takes input (which could be multiple things), does something to that input, and then returns some kind of output.
Take your function for a spin, does it return the correct values?
32 F = 0 C
50 F = 10 C
61 F = 16.11 C
212 F = 100 C
-40 F = -40 C
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)
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 |
See http://www.programiz.com/r-programming/if-else-statement for a good tutorial on if/else statements.
if (test_expression) {
statement
}
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"
If the temperature is less than -20 C, print “Don’t bother going out.”
If the temperature is greater than 30, print “I’m moving back to Canada.”
f_to_c <- function(farenheit) {
celsius <- (farenheit - 32) * 5 / 9
return(celsius)
}