Chapter 11 Creating functions.
Functions are use full when you want to reuse steps to complete a process.
In this example we are going to create a function that returns the amount the batting average based on at-bats and hits.
# Create get-average function
## inside the () you must define the input
get_average <- function(at_bats, hits ){
# Create the situation where the player has not had any at-bats. We can not calculate average since we are divding by 0
if(at_bats==0){
average= NA
}
## Create batting average
if(at_bats>0){
average = hits/at_bats
}
### use the return() function to define output.
return(average)
}
## [1] 0.2383721