Using and Creating Functions

  • Create a function (0:20)
  • Source a function (3:00)
  • Using the 'Source and Save' option (4:00)
  • Total length (5:22)

Creating a Function

Although there are many functions in the base package of R, sometimes there may not be a function that does exactly what you want. In this case, R allows users to create their own functions and call upon them. This can be very helpful when you have to perform the same series of steps over and over again in a script. The basic structure of a user-created function is given below:

nameOfFunction <- function(argument1, arguement2, ...){
# code to execute
  • The function starts with the name of the function on line 1. This will be the name called each time the user wants to execute the created function. In this case, the name of the function is nameOfFunction.
  • Next, the function called function() is called. This is not a user defined function, but is one that is defined in the R base package. This means that any R user can use this function without downloading third party packages.
  • The arguments between parentheses after function are objects and/or variables necessary to execute the code.
  • The open curly bracket indicates where the internal code of the function will start there is no limit to the length of this code.
  • Before the the final curly bracket, return() is called to indicate the output of the function.
  • The closing curly bracket signifies the end of the function.

Below is an example of a user-created function:

# A function to calculate the sum from 1 to a specified number sumTo <- function(number){ x <- 0
for (i in 1:number){
x <- x + i
} return(x)
} # example call of the function after it has been defined
  • The code within the function begins by first setting the variable x equal to zero. This is called 'space allocation.'
  • Second, a for loop takes the sum of everything from one through the number specified (1 + 2 + 3 + ... + number)
  • Finally, return() is used so that the function will output the result from the for loop, and the final curly bracket tells R that the function call has ended.
  • The last line in the example is a call of the now predefined sumTo() function using the input value of 5, from which the value 15 will be returned since 1+2+3+4+5=15.

Sourcing a Function

Similar to saving R scripts as external files, you can also save R functions. By saving an R function as an external file, you can then call your function in other scripts that you write. However, before you can call your function you first have to load your function into your Global Environment. This is called sourcing a function, which is done by using the source() function in R. For instance, let's assume that the above example function sumTo() has been saved in a folder on your Desktop call R_is_Awesome and you would like to use this function without having to copy/paste the actual lines of code into your current script. In that case, you could do the following:

# first source the function to load it into your Global Environment

# then you can call the function
Note: The only argument of source() is a character string formed by combining the file path to the function and the name of the function including the .R file type.

Ready take your skills to the next level?

Proceed to the advanced tutorials.

Online resources are available for additional help.