Arguments in R Programming Language – Get a Deep Insight!

Job-ready Online Courses: Click for Success - Start Now!

With this tutorial, you will get a complete understanding of R function arguments. Firstly, we will discuss about the arguments in R language and process to add more arguments in R. You will also learn to add a mult argument and default value in R and usage of dots argument, function as an argument and anonymous functions in R.

Arguments in R Programming

Arguments in R Programming Language

Arguments are always named when you define a function. When you call a function, you do not have to specify the name of the argument. Arguments are optional; you do not have to specify a value for them. They can have a default value, which is used if you do not specify a value for that argument yourself. You can use as many arguments as you like, there is no limit to the number of arguments. An argument list comprises of comma-separated values that contain the various formal arguments.

Before moving further, you must check the R Recursive Function Tutorial

Adding More Arguments in R

To pass values to a function, you can use R arguments as many as needed. First, we will create our generic function addPercent as follows:

addPercent <- function(x, mult = 100, ...){
 percent <- round(x*mult, ...)
 paste(percent, "%", sep = "")
}

The addPercent function converts the value to a percentage. When the calculated numbers are in percentage format, then first you will have to divide these numbers by 100 to get the correct result.

> percentages <- c(58.23, 120.4, 33)
> addPercent(percentages/100)

Output:

Adding More Arguments in R

This is how more arguments can be added to a function.

That is quite a way around, but you can avoid this by adding another argument to the function that controls the multiplication factor.

Don’t forget to explore the Numeric and Character Functions in R

Adding the mult Argument in R

Now, we know that to add extra arguments, we need to include them between the parentheses after the function keyword. We separate the arguments with a comma. Let’s discuss it with an example:

The code to add the mult argument is shown below:

addPercent <- function(x, mult ){
 percent <- round(x*mult,  digits = 1
 paste(percent, "%", sep = ""
}

Output:

Adding mult argument in R

Adding a Default Value in R

Specifying a default value for an argument helps you drop the task of specifying a value every time you make a call to the function.

Adding an extra argument gives you more control over what the function does, but it also introduces a new problem – If you do not specify the mult argument in the addPercent function, it will give an error as “mult is missing”.

R has no way of knowing which number you want to multiply by x, so it stops and tells you that it needs more information.

You can specify default values for any disagreements in the argument list by adding the = sign and default value after the respective argument.

You can specify a default value for argument mult to avoid specifying mult=100 every time.

addPercent <- function(x, mult = 100){
percent <- round(x * mult, digits = 1)
paste(percent, “%”, sep = “”)
}

Output:

Adding default value in R

Wait! Do you know about R Packages

Using the Dots Argument in R

The special type of argument ‘…’ can contain any number of supplied arguments. It is used for a variety of purposes and allows you to write a function that takes an arbitrary number of arguments.

If you have several arguments and you pass them to other functions inside the body, you will end up with a long list of arguments. Therefore, rather than this, you can use the dots argument.

You use the dots argument by adding it at the end of the argument list of your own function, and at the end of the arguments for the function, you want to pass the arguments to.

Let us see how we can use dots argument in R:

addPercent <- function(x, mult = 100, ...){  
percent <- round(x * mult, ...)
paste(percent, “%”, sep = “”)
}

Output:

Using Dots Argument in R

R allows you to use the dots argument in more than one function within the body. But, before passing arguments to more than one function in the body, you have to be sure that this will not cause any trouble. R passes the extra arguments to each function and complains about the resulting mess afterwards.

Using Functions as Arguments

In R, you can pass a function as an argument. You can also pass function code to an argument.

Then, you can assign the complete code of a function to a new object.

In the above example of rounding the value, you can pass the round() function as an argument to addPercent() function as below:

addPercent <- function(x, mult = 100, FUN = round, ...){      
percent <- FUN(x * mult, ...)    
paste(percent, “%”, sep = “”)
}

Output:

Using Functions as Arguments

Spare some time to check – List of R Vector Functions

Using Anonymous Functions in R

Any function which does not have a name is called an anonymous function. They can be used for 1 liner code. You can add code as an argument in the anonymous function.

In the preceding example, you can use any function you want for the FUN argument. In fact, that function does not even need to have a name, because you copy the code as it is. So, instead of giving a function name, you can add the code as an argument in the form of a nameless or anonymous function.

Let us see this with an example:

Suppose, you have the quarterly profits of your company in a vector as follows:

> profits <- c(2100, 1430, 3580, 5230)

And, you want to report how much profit was made in each quarter relative to the total for the year. For this, you will have to use your new addPercent() function. To calculate the relative profits, you could write a rel.profit() function as follows:

>rel.profit <- function(x) round(x / sum(x) * 100)

Instead of using the function name, you can use the function body itself as an argument.

> addPercent(profits,FUN = function(x) round(x / sum(x) * 100))

Output:

Using Anonymous Functions in R

In some cases, this construct with anonymous functions is useful, especially when you want to use functions that can be only written in a little code and are not used anywhere else in your script.

Summary

We have learned about all the arguments that are used in the R programming language. As you can see, they are very useful and it also reduces the hassles of programmers.

Get ready to play an amazing R Programming Online Quiz!

If you have come across with any questions in your mind, feel free to ask in the comment section below.

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google

courses

DataFlair Team

The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.

Leave a Reply

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