<- function(x) {
square_it <- x * x
square return(square)
}
Creating Functions
User-defined Functions
One of the great strengths of R is the user’s ability to add functions. Sometimes there is a small task (or series of tasks) you need done and you find yourself having to repeat it multiple times. In these types of situations, it can be helpful to create your own custom function. The structure of a function is given below:
<- function(argument1, argument2) {
name_of_function
statements or code that does somethingreturn(something)
}
- First you give your function a name.
- Then you assign value to it, where the value is the function.
When defining the function you will want to provide the list of arguments required (inputs and/or options to modify behaviour of the function), and wrapped between curly brackets place the tasks that are being executed on/using those arguments. The argument(s) can be any type of object (like a scalar, a matrix, a dataframe, a vector, a logical, etc), and it’s not necessary to define what it is in any way.
Finally, you can “return” the value of the object from the function, meaning pass the value of it into the global environment. The important idea behind functions is that objects that are created within the function are local to the environment of the function – they don’t exist outside of the function.
Let’s try creating a simple example function. This function will take in a numeric value as input, and return the squared value.
Once you run the code, you should see a function named square_it
in the Environment panel (located at the top right of Rstudio interface). Now, we can use this function as any other base R functions. We type out the name of the function, and inside the parentheses we provide a numeric value x
:
square_it(5)
[1] 25
Pretty simple, right? In this case, we only had one line of code that was run, but in theory you could have many lines of code to get obtain the final results that you want to “return” to the user.
Do I always have to
return()
something at the end of the function?In the example above, we created a new variable called
square
inside the function, and then return the value ofsquare
. If you don’t usereturn()
, by default R will return the value of the last line of code inside that function. That is to say, the following function will also work.<- function(x) { square_it * x x }
However, we recommend always using
return
at the end of a function as the best practice.
We have only scratched the surface here when it comes to creating functions! We will revisit this in later lessons, but if interested you can also find more detailed information on this R-bloggers site, which is where we adapted this example from.
Exercise
The materials in this lesson have been adapted from work created by the HBC and Data Carpentry, as well as materials created by Laurent Gatto, Charlotte Soneson, Jenny Drnevich, Robert Castelo, and Kevin Rue-Albert. These are open access materials distributed under the terms of the Creative Commons Attribution license (CC BY 4.0), which permits unrestricted use, distribution, and reproduction in any medium, provided the original author and source are credited.