How to Use Arguments When You Transform a Script in R

There are multiple ways to use arguments in R. Here are some ways you can specify arguments in a function call when transforming a script.

  • Arguments are always named when you define the function. But when you call the function, you don’t have to specify the name of the argument if you give them in the order in which they appear in the argument list of a function.

  • Arguments can be optional, in which case you don’t have to specify a value for them.

  • Arguments can have a default value, which is used if you didn’t specify a value for that argument yourself.

Not only can you use as many arguments as you like, but you can very easily pass arguments on to functions inside the body of your own function with the simply genius dots argument. Fasten your seat belts — we’re off to make some sweet R magic.

Add more R arguments

The argument list of the addPercent() function doesn’t really look much like a list yet. Actually, the only thing you can do for now is tell the function which number you want to see converted. It serves perfectly well for this little function, but you can do a lot more with arguments than this.

The addPercent() function automatically multiplies the numbers by 100. This is fine if you want to convert fractions to percentages, but if the calculated numbers are percentages already, you would have to divide these numbers first by 100 to get the correct result, like this:

> percentages <- c(58.23, 120.4, 33)
> addPercent(percentages/100)
[1] "58.2%" "120.4%" "33%"

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

Add the mult argument in R

You add extra arguments by including them between the parentheses after the function keyword. All arguments are separated by commas. To add an argument mult that controls the multiplication factor in your code, you change the function like this:

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

Now you can specify the mult argument in the call to addPercent(). If you want to use the percentages vector, you use the addPercent() function, like this:

> addPercent(percentages, mult = 1)
[1] "58.2%" "120.4%" "33%"

Add a default value in R

Adding an extra argument gives you more control over what the function does, but it introduces a new problem. If you don’t specify the mult argument in the addPercent() function, you get the following result:

> addPercent(new.numbers)
Error in x * mult : 'mult' is missing

Because you didn’t specify the mult argument, R has no way of knowing which number you want to multiply x by, so it stops and tells you it needs more information. This means you would have to specify mult=100 every time you used the function with fractions. Specifying a default value for the argument mult takes care of this.

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

To get the wanted default behavior, you adapt addPercent() like this:

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

Now the argument works exactly the same as arguments with a default value from base R functions. If you don’t specify the argument, the default value of 100 is used. If you do specify a value for that argument, that value is used instead. So, in the case of addPercent(), you can now use it as shown in the following example:

> addPercent(new.numbers)
[1] "82.2%" "2.5%" "162%" "40%"
> addPercent(percentages, 1)
[1] "58.2%" "120.4%" "33%"
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com