Functions In R

This article gives an overview of functions in R, how to use them, and a few examples of functions in R programming.

In my previous few articles on R, I have explained about Loops in R, If Else Statement in R and Variables And Data Types in R. In this article, let us see what a function is, some functions in R, and a few examples of functions. If you are completely new to the R language, then please begin by reading "Introduction to R and RStudio" and the previously mentioned articles.
 

What is a Function? 

 
A function is a block of reusable code which performs a specific task. There are certain tasks which we have to do several times at several instances. So, instead of writing the code several times, functions allow us to write the code one time and call the function wherever required. 
 
Functions in R can be built-in and user-defined. Built-in functions are ones which are already defined in R. You can just call them and use them. The builtins() function gives a list of all built-in functions in R. Let us see a few commonly used built-in functions in R.
 

print() function 

 
The print function prints the argument values on the console. Any object which is passed in the parenthesis() which is present immediately after the function name is an argument. 
  1. print("Learning R Functions")  
Run the above code and "Learning R Functions" would be printed in the console. In this case, the argument is the string "Learning R Functions".
 
Try running the print() function without any argument and observe the result. You must get an error as an argument is missing. This is because the print() function is defined to have an argument.
 
Similarly, try running the print() function with any random object which is not defined. Type print(x) and you will get an error that "x" is not defined.
 
Now, store some value in x and try printing it as shown below. 
  1. x<-5  
  2. print(x)  
  3.   
  4. y<-"Big B"  
  5. print(y)  

c() function

 
c() is one of the most commonly used functions in R. It is used to create an array/vector of data of the same data type.
  1. x<-c(12,34,23,55)   
The above code creates an array of the numbers 12,34,23,55 and stores them in object x. Here, c() defines the array.
 
Now, run the function without any arguments. You will not get any error this time but you will get the value NULL.
 

seq() function

 
The seq() function generates a sequence of numbers on the basis of the arguments passed.
  1. x<-seq(1,10,3)  
  2. x  
  3. length(x)  
  4. mode(x)  
The above code generates a series "1 4 7 10".
 
The first argument in seq() function is from. This indicates the number from which the series must start.
 
The second argument in seq() function is to. This indicates the number at which the series must end.
 
The third argument in seq() function is by. This indicates the difference of the 2 consequent numbers in series.
 
In this case, the difference is 3. The series begins at 1 and ends at 10.
 

mean() function

 
The mean() function is used to calculate the average. 
  1. x<-seq(1,3)  
  2. print(x)  
  3. mean(x)  

median() function

 
The median() function is used to find the middle most values in a collection.
  1. x<-seq(1,3)  
  2. print(x)  
  3. median(x)  
There are a lot of more built-in functions in R. But, I guess the above examples gave you a brief overview of what are functions and how to use built-in functions in R. 
 

What are user-defined functions?

 
R allows us to create our own functions as per our logic and use them as and when required. Such functions are known as user-defined functions in R. 
 
We need to define the function and then call it.
 
Syntax
 
FunctionName <- function(argument1, argument2, ...) {
Function body
}
 
Let us understand this with an example.
 
Function Definition
  1. PrintGreeting <- function(){  
  2.   print("Hello, Good Morning")  
  3. }  
Here, the function name is PrintGreeting. This is followed by an assignment operator which means assign the function to this object.
 
The word function() indicates we are creating a function. This is followed by curly brackets and the function body is within the brackets.
 
In the above example, the function body consists of only a single line of code. There can be several lines of code. In this case, there is a single command which will print the greetings message whenever the function is called.
 
Also, as you can observe that we can not pass any arguments in this function. This is an argument-less function. 
 
Run the above code and the function is created. Now, let's see if the function works. For this, we need to call the function. 
 
Calling the function
  1. PrintGreeting()  
Simply, you need to type the function name followed by parenthesis and run the code.
 
Let us now see the same example with some modification to understand how to define and call functions with parameters.
 
Function Definition
  1. PrintGreeting <- function(a){  
  2.   paste("Hello, Good Morning ",a)  
  3. }  
Here, you can see the argument "a" in function which is used in the function body too. The above function will append the value of "a" along with the greeting message.
 
Calling the function
  1. name="John"  
  2. PrintGreeting(name)  
In the above code snippet, the name is assigned value "John" and the PrintGreeting() function is called with the argument name. 
 
On running the above code, you will get the output as "Hello, Good Morning John".
 
I hope the example was simple and understandable. As, I passed a single argument in the function above; in a similar way, multiple arguments can also be passed if we define the function which accepts multiple parameters. 
 
This article described functions in R in a very simplistic manner. A lot more can be done using the various functions in R. Try creating functions with arguments for adding or subtracting 2 numbers and experiment with code. Unless you practice and try, you will not understand. Stay tuned for more articles on R.
 
Happy learning!