Functions

Sometimes the operations that we want to perform are not built-in in R. In this case, we might consider creating our own functions.

A function is a map from a set of values (in the preimage) to another set of values (in the image) by a certain rule. Inputs of the function can be any type of object (eg. number, string, vector, etc.), and so are the outputs.

3.6 A Basic Example

Suppose we want to create a function that takes three numbers as inputs and returns their sum. Let’s call this the “summation” function.

summation <- function(x, y, z) {        
  a <- x + y + z
  
  a               
}

summation(x = 1, y = 2, z = 3)
## [1] 6

In the parenthesis, specifies the input variables. The last part tells R what the outputs are.

3.7 Functions with control flows

We can also incorporate control flows and other logical structures into a function. Now we want to create a function that ranks 3 numbers, from smallest to largest:

rank <- function(x, y, z) {
  if (x < y) {
    if (y < z) {
      a <- c(x, y, z)
    } else {
      if (x < z) {
        a <- c(x, z, y)
      } else {
        a <- c(z, x, y)
      }
    }
  } else {
    if (x < z) {
      a <- c(y, x, z)
    } else {
      if (y < z) {
        a <- c(y, z, x)
      } else {
        a <- c(z, y, x)
      }
    }
  }
  
  a
}

rank(x = 1, y = 10, z = 5)
## [1]  1  5 10
new <- rank(x = 9, y = 8, z = 7)

3.8 Logical/String as inputs

Functions can also take inputs of logical statements (i.e. TRUE or FALSE) and strings. The following example is a function that performs one of the following operations between two objects up to user’s choice: addition, multiplication, and division.

operation <- function(x, y, op = "add") {
  if(op == "add"){
    z = x + y
  } else {
    if (op == "mult") {
      z = x * y
    } else {
      if (op == "subtraction"){
        z = x - y
      }
    }
  }
  
  z
}

3.9 Multiple outputs as a list

Suppose we want to create a function that takes an input vector of observations and returns both the mean and the standard deviation. We can put the outputs in a list.

central_tendency <- function(x) {
  m = mean(x)
  s = sd(x)
  
  results <- list(average = m, 
                  standard_deviation = s)
  
  results
}

my_vec <- 1:100
central_tendency(my_vec)

To refer to part of the output, use the ‘$’ sign:

cent <- central.tendency(x)
cent$average
cent$standard_deviation

3.10 Your turn

  1. Suppose you want to create a function named variance that computes the variance (\(S^2\)) of a set of observations x, either correcting for the bias \((n-1)\) or not \((n)\) up to the user’s choice. The default uses the corrected variance \(SS / (n-1)\).

hints:

  • The function should have two arguments (x, correct).

  • The sample size n can be obtained by length(x).

  • Write an if/else statement inside the function.

    • If the argument correct == TRUE, compute the variance by \(S^2_X = \frac{\sum_{i=1}^{n}{(X-\bar{X})^2}}{n-1}\), or else, \(S^2_X=\frac{\sum_{i=1}^{n}{(X-\bar{X})^2}} {n}\).
  • You can compute the sum of squares \(SS = \sum_{i=1}^{n}(X-\bar{X})^2\) by:

SS = sum((x - mean(x))^2)