Functions and Loops

Author

Sven Rieger

Last update on

October 15, 2024

Writing functions

Writing functions is the best way to enhance your data processing skills. Functions allow you to automate tasks that are needed to be repeated more than 2-times. This sections gives a brief introduction in writing your own functions. There are other sources that cover this topic in much more detail (see R for Data Science by Hadley Wickham & Garrett Grolemund).

Example: We want to calculate the mean, standard deviation and range of two variables.

The copy/paste way.

set.seed(999)

x <- rnorm(50)
y <- rnorm(50)

mean(x)
sd(x)
range(x)

mean(y)
sd(y)
range(y)

Lets put it a function.

  1. Pick a name for the function (here: myFirstFunction)
  2. Define the inputs and arguments of the function. These are written within the regular brackets (...) (here the input is: variable)
  3. Write the code in the body of the function which is between the curly brackets { }.
    • Calculate the mean, the standard deviation and the range
    • Store it in a named list (for what a named list is useful see below)
    • return the named list
myFirstFunction <- function ( variable ) {
  
  fMEAN <- mean(variable)
  fSD <- sd(variable)
  fRANGE <- range(variable)
  
  fOut <- c("Mean" = fMEAN,
            "SD" = fSD,
            "Min" = fRANGE[1],
            "Max" = fRANGE[2])
  
  return(fOut)
  
}


myFirstFunction(variable = x)
      Mean         SD        Min        Max 
-0.2576685  0.9897502 -2.1137370  2.3826642 

Loops

some text about loops

To execute the function on both variables…

sapply(list(x, y),
       function(myVar) myFirstFunction(variable = myVar),
       simplify = F)
[[1]]
      Mean         SD        Min        Max 
-0.2576685  0.9897502 -2.1137370  2.3826642 

[[2]]
       Mean          SD         Min         Max 
 0.04328125  0.87849494 -2.06482325  1.74441160 
for (myvar in list(x,y)) {
  print(
    myFirstFunction(variable = myvar)
  )
}
      Mean         SD        Min        Max 
-0.2576685  0.9897502 -2.1137370  2.3826642 
       Mean          SD         Min         Max 
 0.04328125  0.87849494 -2.06482325  1.74441160