R Functions

Dec 21, 2023

DataSciencePursuit

In this section, we will cover functions.

What are functions?

Functions are reusable pieces/blocks of code that perform a defined task.

They are most useful when you need to repeat tasks. Think about hand washing clothes vs how much easier it is to use a washing machine. Functions have a similar effect.

In this section, we will learn how to define and call functions. The process of creating a function is referred to as function definition and using a function is referred to as calling a function. We will also cover how to pass arguments to functions, how to store the results in variables and lastly, we will look at built-in functions.

Defining a function

All functions need to be defined (created) first before they are called (used).

To define a function you need the following syntax:

the.functions.name <- function( arg1,arg2,… ){

the functions body

}

The body of the function is R code to complete the specific task.

Arguments

Arguments are values/data that a function can take. In other words, arguments are inputs that can be passed to a function. Arguments for a washing machine would be the clothes, soap and water.

Functions can have any number of arguments, including no arguments. When there are multiple arguments, each argument should also be separated by a comma as shown in the syntax above.

Let’s create a simple function that exponentiates the number we give it.

# defining the power function
power <- function(base,exponent){ 
  base^exponent
}

The arguments in the function definition are placeholder variables that are filled in when calling the function.

Calling a function

To call a function, you need either of the syntax :

the.functions.name(data1,data2,…)

or

the.functions.name(arg1 = data1, arg2 = data2,…)

When calling a function, if you do not specify which data goes with what argument, R will assume you have them in the same order as in the function definition.

# calling the power function
power(2,3)
[1] 8
power(3,2)
[1] 9
power(base = 2,exponent = 3)
[1] 8
power(exponent = 3,base = 2)
[1] 8

By default, functions will return the value of the last evaluated expression (that is what is being displayed on the screen).

Default arguments

In general, when calling functions you need to include the same number of arguments as those defined in the function. The exception is when there are default arguments.

power(2)
Error in power(2) : argument "exponent" is missing, with no default

We will define the power function again, but this time we will give the exponent argument a default value of 2. It is a simple modification to the exponent argument.

# define power function with default exponent
power <- function(base,exponent = 2){ 
  base^exponent
}

# call
power(2)
[1] 4

Storing returned values

The value/data that functions return can be stored in a variable.

Let’s see an example, we will name our variable two.squared, and then store the result of power(2), which is 4.

two.squared <- power(2)
two.squared
[1] 4

Our variable can now be used elsewhere.

We have already covered variables, so will not add much more detail here.

Functions as arguments

Since functions return values, we can also pass functions as arguments to other functions, without having to store them in variables first.

power(power(2))
[1] 16

R first evaluates the argument (power(2)) and then the main function (outer power function).

Now you know quite a bit about defining and calling functions, which is awesome. But you don’t always have to define functions, R has a lot of built-in functions which is great because the best code is code that someone else wrote.

Built-in functions

Built-in functions are functions already defined within R. Since built-in functions are already defined, we just need to call them. This is like when a machine machine comes with the house/apartment. Here are 3 examples of built-in functions in R.

mean function

Returns the average of the arguments given.

mean(2,4)
[1] 2

This and other functions are used a lot for statistics and in turn, data science.

R chunks display outputs to the screen except in certain cases. For example, they do not display code inside functions and loops (to be covered later). This is when you can use the print function. The print function displays its arguments to the screen, just like R chunks but it works in all cases. Particularly inside functions and loops.

# Define
numbers <- function(){ 
  1
  2
  3
}

# Call
numbers()
[1] 3
# Define
numbers <- function(){ 
  print(1)
  print(2)
  3
}

# Call
numbers()
[1] 1
[1] 2
[1] 3

return function

Is used in functions to return a specific value (other than the default last evaluated expression). However, once the return function is run, the main function will stop execution.

# function definition
numbers <- function(){ 
  1 
  return(2) # functions will stop here 
  3         # 3 will not be run
}

numbers() #returns the expression we want
[1] 2

You’ve probably noticed from these examples that we can use functions inside function definitions as well.

We will see many other useful built-in functions in later sections.