A function is a set of statements organized together to perform a specific task. R has a large number of built-in functions but we may as well create our own, user-defined functions.


Built-in Functions

R has many in-built functions which can be directly called without defining them beforehand. In base R you find functions such as seq(), mean(), max(), min(), sum(), paste(), length() and sample(), among others.

s <- seq(1:5000)
mean(s)
## [1] 2500.5
max(s)
## [1] 5000
min(s)
## [1] 1
sum(s)
## [1] 12502500
paste("The sequence 's' contains", length(s), "components")
## [1] "The sequence 's' contains 5000 components"

A function consists of different parts:

Consider the sample() function, which takes a random sample of the specified size from the elements of a vector (either with or without replacement). Type help("sample") into your console for further information.

We may ask R for the arguments of the sample() function by using the args() command.

args(sample)
## function (x, size, replace = FALSE, prob = NULL) 
## NULL

If we want to retrieve the function body we just type the function name (without parenthesis!) into the R console. With respect to our example, we type sample into our console.

sample
## function (x, size, replace = FALSE, prob = NULL) 
## {
##     if (length(x) == 1L && is.numeric(x) && is.finite(x) && x >= 
##         1) {
##         if (missing(size)) 
##             size <- x
##         sample.int(x, size, replace, prob)
##     }
##     else {
##         if (missing(size)) 
##             size <- length(x)
##         x[sample.int(length(x), size, replace, prob)]
##     }
## }
## <bytecode: 0x0000022b2973aef8>
## <environment: namespace:base>

In order to apply the sample() function for picking 5 numbers with replacement from the sequence s, which we defined above, we type sample(s, size = 5, replace = TRUE) into the R console.

sample(s, size = 5, replace = TRUE)
## [1] 4748 3115 3682 2310 2926

User-defined Functions

It is very easy to create user-defined functions in R. A function in R is created by using the keyword function. The basic syntax of a function definition is as follows:

function_name <- function(arg_1, arg_2, ...) {
   Function body
}

Let us create a function to print the cube of the number 3 \((3^3)\). Note that this function does not need any arguments.

print.cube3 <- function() {
  rv <- 3^3 # return value
  print(rv)
}

In order to call the function print.cube3() we simply type print.cube3() into the R console.

print.cube3()
## [1] 27

This function is for sure not of much use, hence we extend the function and compute the cube of any scalar given by the user \((x^3)\).

print.cube <- function(s) {
  cubed <- s^3
  print(cubed)
}

In order to call the function print.cube(), this time we have to provide the argument s.

print.cube(2)
## [1] 8

Calling a Function with Argument Values (by position and by name)

For real life applications functions often have several arguments. It is worth noting, that in R the arguments to a function call can be supplied in the same sequence as defined in the function or they can be supplied in a different sequence but assigned to the names of the arguments.

To showcase this behavior we define a function to do some simple computations.

my_function <- function(a, b, c) {
  result <- a * b - c
  print(result)
}

Now, we can call the function by position of arguments, where a = 2, b = 10 and c = 1. We have to make sure that the numbers are given to the function in correct order.

my_function(2, 10, 1)
## [1] 19

We would get the same result by calling the function by names of the arguments, however, this time we do not need to care about the specific order of the given arguments.

my_function(b = 10, c = 1, a = 2)
## [1] 19

Calling a Function with Default Argument

Another convenient feature of a function is that we can define the value of the arguments in the function definition and call the function without supplying any argument to get the default result. But we can also call such functions by supplying new values of the argument and get non-default result.

# Create a function with arguments.
my_multiply <- function(a = 15, b = 3) {
  result <- a * b
  print(result)
}

Now we can either call the function without giving any argument.

my_multiply()
## [1] 45

Or we may call the the function and provide new values for the arguments.

my_multiply(5, 5)
## [1] 25

 

Exercise: Write a function that calculates the sine (cosine) of an angle provided in degrees! Test your function for 45, 90 and 270 degrees.

### your code here
Show code
sine_deg <- function(x) {
  result <- sin((pi / 180) * x)
  return(result)
}
sine_deg(c(45, 90, 270))
## [1]  0.7071068  1.0000000 -1.0000000

The expected results for sine_deg(45), sine_deg(90) and sine_deg(270) are 0.71, 1 and -1, respectively.


Citation

The E-Learning project SOGA-R was developed at the Department of Earth Sciences by Kai Hartmann, Joachim Krois and Annette Rudolph. You can reach us via mail by soga[at]zedat.fu-berlin.de.

Creative Commons License
You may use this project freely under the Creative Commons Attribution-ShareAlike 4.0 International License.

Please cite as follow: Hartmann, K., Krois, J., Rudolph, A. (2023): Statistics and Geodata Analysis using R (SOGA-R). Department of Earth Sciences, Freie Universitaet Berlin.