Source file ⇒ lec24.Rmd

Functions

Functions are one of the most important constructs in R (and many other languages). They allow you to modularize your code - encapsulating a set of repeatable operations as an individual function call.

Here is an example. R has a function var() that computes the sample variance, usually denoted \(S^2\) which is slightly different than the variance of a list.

\[\mathrm{variance \,\,of \,\,a\,\, list}=\frac{1}{n}\sum_{1}^{n}(x_i-\overline{x})^2\] \[S^2=\frac{1}{n-1}\sum_{1}^{n}(x_i-\overline{x})^2\] Notice that \[\mathrm{variance \,\,of \,\,a\,\, list}==\frac{n-1}{n}S^2\]

Lets write a function that allows you to input a vector and output the variance of the list.

myvar <- function(x){
    n <- length(x)
    return((n-1)*var(x)/n)
}

myvar(c(1,2))
## [1] 0.25
var(c(1,2))
## [1] 0.5

For another example I might want to have a function that simulates rolling a die. Analyze this code line by line with a neighbor.

die_rolling_simulation <- function(n=10){
  myrolls <- c()
  for(i in 1:n){
    roll <- sample(1:6,1)
    myrolls <- c(myrolls,roll)
  }
  return(myrolls)
}

die_rolling_simulation()
##  [1] 3 4 6 6 5 6 2 4 3 3

Anatomy of a function

The syntax for writing a function is

function (arglist) body

Typically we assign the function to a particular name.

myfunc <- function (arglist) body

The keyword function just tells R that you want to create a function.

The arguments to a function are its inputs, which may have default values. For example the arguments of the rnorm() function are mean=0, and sd=1:

args(rnorm)
## function (n, mean = 0, sd = 1) 
## NULL

for example:

rnorm(5,mean=2,sd=4)
## [1] -7.8626402 -0.6854213  7.2046646  9.2474241  1.3379337

Next we have the body of the function, which typically consists of expressions surrounded by curly brackets. Think of these as performing some operations on the input values given by the arguments.

{
    expression 1  
    expression 2
    return(value)
}

The return expression returns a given value.

In the absence of a return expression, a function will return the last evaluated expression. This is particularly common if the function is short. For example, I could write the simple function:

sumofsquares <- function(x, y) sum(x^2+y^2)

sumofsquares(2,3)
## [1] 13

Here I don’t even need brackets {}, since there is only one expression.

in class exercise