Functions in R | Gain Expertise in its Usage with Various Methods!

Now, it’s time to uncover one of the most essential concepts of R programming that is R function. We will cover every aspect related to it in this tutorial. And, I assure you that after completion of this article, you will never scratch your head while using R functions.

R Functions tutorial

So, let’s start the tutorial.

What is R?

R is an open-source and free programming language that provides various facilities for statistical computation and graphics. Due to its wide range of packages, R has become the primary tool for statistical computing by statisticians and data miners for the development of statistical software in order to carry out data analysis as well as data visualisation.

Let us now look at some key capabilities of R:

  • R is easily extensible through functions and extensions.
  • It provides effective data handling and manipulation components.
  • It provides tools specific to a wide variety of data analysis.
  • R has several operators for calculations on arrays and other types of ordered data.
  • It provides advanced visualization capabilities through different tools available.

You must definitely check the R Packages tutorial

Moving from Scripts to R Function

R function provides two major advantages over the script:

  • Functions can work with any input. You can provide diverse input data to the functions.
  • The output of the function is an object that allows you to work with the result.

How to Create a Script in R?

We will now learn the methodology of creating a script in R.

As we know, R supports several editors. So the script can be created in any of the editors like Notepad, MS Word or Word Pad and can be saved with R extension in the current working directory.

Now to read the file in R, source function can be used.

For example, if we want to read the sample.R script in R, we need to provide below command:

source("sample.R")

This will read the file sample in R.

In order to create a script, first, open a script file in the editor mode and type the required code.

We will create a script that takes in input in the form of fractions and converts it into a percentage by further rounding it to one decimal digit.

> #Author DataFlair
> frac <- c(0.452, 1.5642, 0.84520)
> percentage <- round(frac * 100, digits = 1)
> out <- paste(percentage, "%", sep = "")
> print(out)

That is, you need to give values that you want to convert to a percentage as input and then convert it into percentage and round off to required places. Then put the % sign and display the answer.

Save the above script as script file with any name for example pastePercent.R.

pastepercent.R input - R Functions

Now you can call this script on the console with the help of source command which we have already seen.

source(‘pastePercent.R’)

Output:

pastepercent.R output - R Functions

This is how a script is written and executed in R.

Wait! Have you checked the Control Structures in R

Transforming the Script into R Function

Now, we are going to see how to convert R script into the function in R.

Firstly, define a function with a name so that it becomes easier to call an R function and pass arguments to it as input.

The R function should be followed by parentheses that act as a front gate for your function and between the parentheses, arguments for the function are provided.

Use the return() statement that acts as a back gate of your function.

The return() statement provides the final result of the function that is returned to your workspace.

We will see this with an example below.

Let us now see how we can convert the script that we had written earlier to convert values into percentage and round off into an R function.

Percent_add <- function(frac){
  percent <- round(frac * 100, digits = 1)
  out <- paste(percent, "%", sep = "%")
  return(out)
}

Output:

Tranforming the script into R function

The keyword function defines the starting of function. The parentheses after the function form the front gate, or argument list of the function. Between the parentheses are the arguments to the function. In this case, there is only one argument.

The return statement defines the end of the function and returns the result. The object put between the parentheses is returned from inside the function to the workspace. Only one object can be placed between the parentheses.

The braces, {} are the walls of the function. Everything between the braces is part of the assembly line or the body of the function. This is how functions are created in R.

Explore the different arguments in R programming

Using R Function

After transforming the script into an R function, you need to save it and you can use the function in R again if required.

As R does not let you know by itself that it loaded the function but it is present in the workspace, if you want you can check it by using ls() command.

Now as we know what all functions in R are present in the memory and we can use it when required.

For example, if you want to create percentage from values again, you can use add percent function for the same as below:

#Author DataFlair
ls()
new.vector <- c(0.8223, 0.02487, 1.62, 0.4)
Percent_add(new.vector)

Output:

using R Function

Using the Function Objects in R

In R, a function is also an object and you can manipulate it as you do for other objects.

You can assign a function to the new object using below command:

percent_paste <- Percent_add

Now percent_paste is a function as well that does exactly the same as Percent_add. Note that, you do not add it after parenthesesPercent_add in this case. If you add the parentheses, you call the function and put the result of that call in percent_paste. If you do not add the parentheses, you refer to the function object itself without calling it.

percent_paste

Percent_add <- function(frac){
  percentage <- round(frac * 100, digits = 1)
  out <- paste(percentage, "%", sep = "")
  return(out)
}

print(Percent_add(new.vector))

Code Display:

Using the Function Objects in R - Input

The output of the above code is as follows:

Using the Function Objects in R - Output

Don’t forget to check the R Vector Functions Tutorial

Reducing the Number of Lines in R

As of now, we have seen how to convert the script into a function and how to assign a function to the new object. All these include a large number of lines to be written.

So, let us now understand how we can reduce the number of lines in R.

There are basically two ways of doing it:

  • Returning values by default
  • Dropping {}

Let us see the above ways in detail below:

1. Returning Values by Default in R

Till now, in all the above code, we have written return() function to return output. But in R, this can be skipped as by default, R returns the value of the last line of code in the R function body.

Now, the above code will be:

#Author DataFlair
Percent_add <- function(fac){ 
percentage <- round(fac * 100, digits = 1) 
paste(percentage, "%", sep = "")}

Output:

Returning Values by Default in R

You need return if you want to exit the function before the end of the code in the body.

For example, you could add a line to the Percent_add function that checks whether fac is numeric, and if not, returns NULL, as shown in the following table:

#Author DataFlair
Percent_add <- function(frac){
if( !is.numeric(frac) ) return(NULL) 
percentage <- round(frac * 100, digits = 1) 
paste(percentage, "%", sep = "")}

Output:

Percent_add function

2. Dropping the {}

You can drop braces in some cases though they form a proverbial wall around the function.

If a function consists of only one line of code, you can just add that line after the argument list without enclosing it in braces. R will see the code after the argument list as the body of the function.

Suppose, you want to calculate the odds from a proportion. You can write a function without using braces, as shown below:

> odds <- function(x) x / (1-x)

Here no braces are used to write the function.

Any doubts till now? Share your queries in the comment section.

Scope of R Function

Every object you create ends up in this environment, which is also called the global environment. The workspace or global environment is the universe of the R user where everything happens.

There are two types of R functions as explained below:

1. External R Function

If you use an R function, the function first creates a temporary local environment. This local environment is nested within the global environment, which means that, from that local environment, you also can access any object from the global environment. As soon as the function ends, the local environment is destroyed along with all the objects in it.

If R sees any object name, it first searches the local environment. If it finds the object there, it uses that one else it searches in the global environment for that object.

2. Internal R Function

Using global variables in an R function is not considered a good practice. Writing your functions in such a way that they need objects in the global environment is not efficient because you use functions to avoid dependency on objects in the global environment in the first place.

The whole concept behind R strongly opposes using global variables used in different functions. As a functional programming language, one of the main ideas of R is that the outcome of a function should not be dependent on anything but the values for the arguments of that function. If you give the arguments for the same values, you will always get the same results.

This characteristic of R may strike you as odd, but it has its merits. Sometimes you need to repeat some calculations a few times within a function, but these calculations only make sense inside that function.

The below example shows the usage of internal function:

#Author DataFlair
calculate_func <- function(data1, data2, data3){
  base_min <- function(z) z - mean(data3)
  base_min(data1) / base_min(data2)
}

Output:

usage of R internal function

The code for calculate.eff function is shown below:

> #Author DataFlair
> d1 <- c(3.24, 2.21, 1.45)
> d2 <- c(4.65, 5.12, 4.23)
> d3 <- c(0.11, 0.20, 0.49, 0.28)
> calculate_func(d1,d2,d3)

Output:

 calculate.eff function

A closer look at the R function definition of base_min() shows that it uses an object control but does not have an argument with that name.

Do you know about Matrix Function in R

Finding the Methods behind the Function

It is easy to find out the function you used in R. You can just look at the function code of print() by typing its name at the command line.

In order to display the info code of the print() function, we proceed as follows:

print

Output:

print() function in R

1. The UseMethod() Function

How can that one line of code in the print() function do so many complex things, such as printing vectors, data frames, and lists, all in a different way? The UseMethod() function contains the answer. This answer plays the role of a central function in the main generic function system.

The UseMethod() function moves along and looks for a function that can deal with the type of object that is given as the argument x.

Suppose you have a data frame that you want to print. The object that was passed as an argument will be printed using the print.data.frame() function that will be first looked up by R.

The other functions are searched through a thorough search of another function. The procedure is then started with a print which is then followed by the object type and the dot.

The function print.data.frame() can also be called inside the code. This is explained in the coming section.

2. Calling Functions Inside Code

You can also call the function print.data.frame() yourself.

Below is the example for the same:

> #Author DataFlair
> small_data <- data.frame(c1 = 1:2, c2 = 2:1)
> print.data.frame(small_data)

Output:

Calling Functions Inside Code

Using Default Methods in R

R provides the feature to create an object with the names that are already used by R. It is possible with the use of default keyword.

R will ignore the type of the object in that case and just look for a default method if you use the default keyword with the name of an object.

Below example explains it:

> #Author DataFlair
> print.default(small_data)

Output:

Using Default Methods in R - print default

Summary

We discussed different methods to use R functions. Also, we learned about reducing the number of lines along with the default methods in R. At last, we explored the scope of R function.

Now, it’s time to learn about the R Recursive Function

If you have any query regarding R function, do ask in the comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.