Control structures I: functions

Functions

When to use functions

  • for common tasks and computations
  • helps to structure code
  • to reduces code redundancies
  • for integration into packages with documentation

function syntax

Functions are created the same way as you would create a new variable. You assign (<- / =) a function to a variable name which in turn will be the name of the function. In the parenthesis you define the names of the arguments (in the example below arg1 and arg2) of function. These arguments are basically placeholders i.e. variables you can use inside the function. Finally the function needs to return something, meaning the result of your function or the output.

myFunction <- function(arg1, arg2){
  X <- arg1 + arg2
  result <- sqrt(X)
  return(result)
}

# executing the function
a <- myFunction(arg1 = 8, arg2 = 10)
a
[1] 4.242641
Task
  • Write a simple function called span which subtracts the lowest value in a vector from the largest value in the vector.
  • Test the function with the vector below.
vec <- c(8,6,7,16,3,8,71)

Generalise your code

The following dataset contains the average summer temperatures per year in Germany. You can download it here.

The code block below calculates the long term average summer temperature between 1960 and 1989 for Bayern. In the following task we want to generalise this code in a way that we can choose for which years and which state we calculate the average temperature.

dwd = read.csv("https://opendata.dwd.de/climate_environment/CDC/regional_averages_DE/seasonal/air_temperature_mean/regional_averages_tm_summer.txt", sep = ";", skip = 1)
knitr::kable(head(dwd))
Jahr summer Brandenburg.Berlin Brandenburg Baden.Wuerttemberg Bayern Hessen Mecklenburg.Vorpommern Niedersachsen Niedersachsen.Hamburg.Bremen Nordrhein.Westfalen Rheinland.Pfalz Schleswig.Holstein Saarland Sachsen Sachsen.Anhalt Thueringen.Sachsen.Anhalt Thueringen Deutschland X
1881 summer 17.13 17.11 17.03 16.39 16.62 16.12 16.24 16.24 16.66 16.90 15.80 17.19 16.20 16.88 16.46 15.95 16.53 NA
1882 summer 16.45 16.44 14.85 14.46 15.02 16.19 15.71 15.72 15.48 15.16 16.10 15.33 15.05 16.02 15.41 14.64 15.33 NA
1883 summer 17.52 17.51 15.87 15.50 16.15 16.66 16.35 16.36 16.37 16.23 16.31 16.42 16.49 17.14 16.57 15.84 16.26 NA
1884 summer 17.00 16.98 16.08 15.35 16.01 16.32 16.20 16.21 16.45 16.52 16.19 16.83 15.92 16.57 16.02 15.33 16.10 NA
1885 summer 17.18 17.17 16.63 16.05 15.84 16.06 15.78 15.78 16.11 16.28 15.53 16.65 16.29 16.63 16.10 15.43 16.18 NA
1886 summer 17.16 17.15 16.04 15.47 15.88 16.05 15.89 15.89 16.17 16.17 15.52 16.42 16.09 16.61 16.05 15.35 16.00 NA
# mean temperature in Bayern between 1960 and 1989
mean(dwd[dwd$Jahr >= 1960 & dwd$Jahr <= 1989, "Bayern"])
[1] 15.82
Task
  • Write a function that calculates the long-term average temperature for a particular region in Germany.
  • The function should have three arguments: the region, the start year and the end year.
  • The function should return the temperature average between these two year for the specified region.