Introduction to Arguments in R Programming Language

1. Objective – R Arguments

In this blog, you will read all about R function arguments. First of all, we will discuss what is Arguments in R language, How to add more arguments in R. This blog also includes how to add a mult argument in R, how to add default value to R. Use of dots argument, function as argument, Anonymous functions in R are also described in this blog.

Introduction to Arguments in R Programming Language

Introduction to Arguments in R Programming Language

2. Arguments in R Programming Language

We have already seen functions in R and packages in R. Let us now see what are R arguments, how to use arguments in R and how to specify R arguments.
Arguments always named when you define a function. When you call a function, you do not have to specify the name of the argument. Arguments can be optional; you do not have to specify a value for them. Arguments 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 on the number of arguments. An argument list is a comma separated list of formal arguments.

3. Adding More Arguments in R

To pass values to a function, you can use R arguments as many as needed.
Let’s see this with an example addPercent() function converts the value to a percentage. When the calculated numbers are in percentage format, then you would have to divide these numbers first by 100 to get the correct result.

> percentages <- c(58.23, 120.4, 33)      // Defines the function
> addPercent(percentages/100)              // Calculates the percentage
[1] “58.2%” “120.4%” “33%”                 // Shows the output of the code

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.

4. Adding the mult Argument in R

We now know that to add extra arguments, we need to include them between the parentheses after the function keyword. With comma, we separate the arguments. Let’s discuss it with example-
The code to add the mult argument is shown below:

addPercent <- function(x, mult){
percent <- round(x * mult, digits = 1)      //Adds the mult argument to control the multiplication factor.
paste(percent, “%”, sep = “”) }

5. 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 error as “mult is missing”.
R has no way of knowing which number you want to multiply x by, so it stops and tells you it needs more information.
You can specify default values for any disagreements in the argument list by adding the = sign and the 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){ //defines function with 2 arguments x and mult
percent <- round(x * mult, digits = 1)
paste(percent, “%”, sep = “”)
}

6. 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. It allows you to write a function that takes an arbitrary number of arguments.
If you have several arguments and you pass on to other functions inside the body, you will end up with a long list of arguments rather than this you can use 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 arguments to.
Let us see how we can use dots argument in R:

addPercent <- function(x, mult = 100, ...){        //Defines the function with two arguments, i.e. x and mult.
percent <- round(x * mult, ...)
paste(percent, “%”, sep = “”)
}

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 this will not cause any trouble. R passes all extra arguments to every function and complains about the resulting mess afterward.

R Quiz

7. Using Functions as Arguments

In R, you can pass a function as an argument. You can also pass function code to an argument.
You can assign the complete code of a function to a new object.
In 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, ...){      //Defines the round() function as arguments inside the addPercent() function.
percent <- FUN(x * mult, ...)       //Defines the function to represent fractional numbers as percentages.
paste(percent, “%”, sep = “”)
}

8. Using Anonymous Functions in R

Any function which does not have a name is called as an anonymous function. These can be used for 1 linear 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. So, instead of giving a function name, you can add the code as an argument as 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)
Suppose you want to report how much profit was made in each quarter relative to the total for the year—for this, you want 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 function name, you can use the function body itself as an argument, as below;

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

This gives output as:
[1] “17%” “12%” “29%” “42%”
In some cases, this construct with anonymous functions is useful, especially when you want to use functions that can be written in only a little code and are not used anywhere else in your script.

See Also-

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.