Arguments in R – Give more Power to your R Functions

This TechVidvan tutorial is designed to give you a practical understanding of how to deal with arguments in R programming.

Functions are handy features in any programming language. They make life easier and reduce redundancy in programs. Yet, without the arguments passed to functions their usefulness is reduced to trivial.

In this R tutorial, we will talk about the role R arguments play in programming. We will learn how they are passed to functions and how they enhance the working of functions in R. So, let get going!

Keeping you updated with latest technology trends, Join TechVidvan on Telegram

What are Arguments in R Programming?

Arguments are inputs that a function requires. They are named while defining a function. Arguments are optional, you only need to define them if the function requires any. A function can have multiple arguments.

The names of the arguments are optional in a function call. When calling a function, you only need to give their values in the order they are in, in the function’s definition.

We can also provide default values for arguments in the function definition. The function uses the default value of the arguments in case they are not provided in the function call.

To start working with R arguments you must have a clear understanding of functions in R

A Function with an Argument

We can pass an argument to a function when we call that function. We just need to give the value of the argument inside the parenthesis after the function’s name. The following is the example of a function with a single argument. The function takes a numeric input and checks whether it is divisible by 3 or not.

Code:

div <- function(int){
  if(int%%3==0){
    return("the number is divisible by 3")
  } else {
    return("the number is not divisible by 3")
  }
}
div(6)
div(7)

Output:

single argument div() - R arguments

Functions with Multiple Arguments

Functions can have multiple arguments. While calling the function, we can either give the values to the arguments with their names or simply give them in the order they are in the definition of the function.

In the above example, we check whether a number is divisible by 3 or not. Imagine that we want to check whether it is divisible by another number instead. We can do this by providing the second number as a second argument. For example:

Code:

div <- function(int, divisor){
  if(int%%divisor==0){
    return(paste(int,"is divisible by",divisor,sep=" "))
  } else {
    return(paste(int,"is not divisible by",divisor,sep=" "))
  }
}
div(6,3)
div(32,4)
div(9,2)

Output:

multiple R arguments div()

Arguments with Default Values

We can provide default values for arguments in the function’s definition. This value is used in case a new value is not provided in the function call.

We can provide a default value for the divisor argument in case the divisor is not provided in the call. For example:

Code:

div <- function(int, divisor=3){
  if(int%%divisor==0){
    return(paste(int,"is divisible by",divisor,sep=" "))
  } else {
    return(paste(int,"is not divisible by",divisor,sep=" "))
  }
}
div(6,2)
div(9)

Output:

default value for R arguments div()

Using the dots operator

The operator, technically known as the ellipsis, allows a function to take arguments that are not predefined in its definition. The ellipsis is useful when we don’t know how many arguments a function may take. For example:

Code:

cate <- function(...){
  l <- list(...)
  paste(l,collapse=" ")
}
cate(9L,  5i, FALSE, "hello", "these", "are", "separate", "arguments", 3, TRUE)

Output:

dots operator cate() arguments in r

In the above example, we use the ellipsis operator to take a variable number of values. We convert them all to characters and then concatenate them into a single string.

Passing Functions as Arguments

Passing default functions

We can also pass functions as arguments to other functions. For example:

Code:

proc <- function(x,FUN){
  return(FUN(x))
}
proc(c(5,6,7,8),sum)
proc(c(2,3,4,5),mean)

Output:

passing functions proc() - R arguments

Passing user-defined functions

We can even pass user-defined functions as arguments to other functions

Code:

proc <- function(x,FUN){
  return(FUN(x))
}
addfive <- function(a){
  return(a+5)
}
proc(c(1,2,3,4,5),addfive)

Output:

passing user-defined functions proc() addfive() - R arguments

Passing function with their arguments

We can use the operator along with other functions to pass arguments that they might need. For example:

Code:

proc <- function(x,FUN,...){
  return(FUN(...,x))
}
proc(c("these","are","separate","elements"),paste
,collapse=" ")

Output:

passing functions with dots operator - R arguments

Note: Here the collapse argument is received by the ellipsis operator. As the paste function uses the collapse argument, R automatically passes the argument to it.

Summary

In this R tutorial, we learned about arguments in R functions. We learned how to use them and how to pass them. We also learned about setting default values for them as well. We looked at functions that can be passed as arguments to other functions.

Arguments make complex problem solving possible with the use of functions in R programming.

Any difficulty working with R arguments? Ask us and our TechVidvan experts will be happy to help you.

Keep Learning ?

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.