How to Use Dimensions to Extract Values from an Array in R
How to Go Beyond Date and Time Basics in R
How to Combine Logical Statements in R

How to Use the Dots Argument in R

There are multiple ways to add arguments in R. The addPercent() function rounds every percentage to one decimal place, but you can add another argument to specify the number of digits the round() function should use in the same way you can for the mult argument. If you have a lot of arguments you pass on to other functions inside the body, you’ may have a long list of arguments.

R has a genius solution for this: the dots (...) argument. You can see the dots argument as an extra gate in your little function. Through that gate, you drop additional resources (arguments) immediately at the right spot in the production line (the body) without the hassle of having to check everything at the main gate.

You normally 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.

To pass any argument to the round() function inside the body of addPercent, you adapt the code of the latter as follows:

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

Now you can specify the digits argument for the round() function in the addPercent() call like this:

> addPercent(new.numbers, digits = 2)
[1] "82.23%" "2.49%" "162%"  "40%"

You don’t have to specify any argument if the function you pass the arguments to doesn’t require it. You can use the addPercent() function as before:

> addPercent(new.numbers)
[1] "82%" "2%"  "162%" "40%"

Notice that the outcome isn’t the same as it used to be. The numbers are rounded to integers and not to the first decimal.

If you don’t specify an argument in lieu of the dots, the function where the arguments are passed to uses its own default values. If you want to specify different default values, you’ll have to add a specific argument to the argument list instead of using the dots.

So, to get addPercent() to use a default rounding to one decimal, you have to use the following code:

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

You don’t have to give the argument in the argument list the same name as the argument used by round(). You can use whatever name you want, as long as you place it in the right position within the body. However, if you can use names for arguments that also are used by native functions within R, it’ll be easier for people to understand what the argument does without having to look at the source code.

R won’t complain if you 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 won’t cause any trouble. R passes all extra arguments to every function, and — if you’re lucky — complains about the resulting mess afterward.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Make a Minimal Reproducible Example to Get Help with R
How to Use read.table() to Import Tabular Data in R
How to Traverse Data with Apply Functions in R
How to Sort and Order Data in R
How to Format and Perform Operations on Dates and Times in R
Advertisement

Inside Dummies.com