Writing Functions in R

Examples

Using the 1990 flight data, define a function called myaveragedelay that takes a 3-letter string (correspding to an airport code) and finds the average departure delays (after removing the NA values) from the DepDelay column. Use myaveragedelay("IND") to print the average departure delays for flights with Origin airport "IND".

Click to see solution
flightDF <- read.csv("/anvil/projects/tdm/data/flights/subset/1990.csv")
myaveragedelay = function(x) {mean(flightDF$DepDelay[flightDF$Origin == x], na.rm=TRUE)}

myaveragedelay("IND")
5.96977225672878

Using the 1990 flight data, define a function called myaveragedelay that takes a 3-letter string (correspding to an airport code) and finds the average departure delays (after removing the NA values) from the DepDelay column. Use myaveragedelay("JFK") to print the average departure delays for flights with Origin airport "IND".

Click to see solution
flightDF <- read.csv("/anvil/projects/tdm/data/flights/subset/1990.csv")
myaveragedelay = function(x) {mean(flightDF$DepDelay[flightDF$Origin == x], na.rm=TRUE)}

myaveragedelay("JFK")
 11.8572741063607

Write a function called monthlydepdelays that takes a year as the input and returns a table of length 12 with the average DepDelay for flights starting at IND in each of the 12 months of that year.

Click to see solution
monthlydepdelays <- function(year) {
  file_path <- paste0("/anvil/projects/tdm/data/flights/subset/", as.character(year), ".csv")
  flights <- read.csv(file_path)
  ind_flights <- subset(flights, Origin == "IND")
  avg_delays <- tapply(ind_flights$DepDelay, ind_flights$Month, mean, na.rm = TRUE)
  return(avg_delays)
}

monthlydepdelays(1990)
1
    7.28277205677707
2
    9.49702660406886
3
    6.92484111633048
4
    4.94985835694051
5
    5.47148703956344
6
    6.01083547191332
7
    4.30737704918033
8
    5.63978201634877
9
    4.45558583106267
10
    4.47372488408037
11
    3.4083044982699
12
    9.76410531972058