do.call & call Functions in R (3 Examples)

 

In this post you’ll learn how to apply the do.call and call functions in the R programming language.

Table of contents:

Let’s just jump right in:

Definitions & Basic R Syntaxes of do.call and call Functions

 

Definitions: Please find the definitions of the do.call and call functions below.

  • The do.call The do.call R function executes a function by its name and a list of corresponding arguments.
  • The call The call R function creates objects of the class “call”.

 

Basic R Syntaxes: You can find the basic R programming syntaxes of the do.call and call functions below.

do.call("any_function", arguments_list)       # Basic R syntax of do.call function
call("any_function", argument1, argument2)    # Basic R syntax of call function

 

In the following, I’ll show three examples for the application of the do.call and call functions in the R programming language.

Example 1: Basic Application of do.call() Function

The following R programming syntax explains how to use the do.call command to execute a function by its name. First, we have to create some example data:

x1 <- 1:10                                    # Example vector
x1                                            # Print example vector
# 1  2  3  4  5  6  7  8  9 10

Our example data is a numeric vector ranging from 1 to 10.

Let’s assume that we want to compute the sum of this vector using the sum function. Furthermore, let’s assume that we only have the name of the sum function stored as character string (i.e. “sum”). Then, we can use the do.call command to execute the sum function as shown below:

do.call("sum", list(x1))                      # Apply do.call
# 55

Note that we had to convert our example data into a list using the list function, because the do.call function takes argument only in the list format.

However, the result of the previous function call is 55 and therefore the same as if we would apply the sum function in its conventional way:

sum(x)                                        # Comparison with conventional syntax
# 55

Looks good!

 

Example 2: Applying do.call with Multiple Arguments

Example 2 illustrates how to use the do.call function with more than one argument. First, let’s modify our example vector:

x2 <- c(x1, NA)                               # Example vector with NA
x2                                            # Print example vector
# 1  2  3  4  5  6  7  8  9 10 NA

The new example vector contains a numeric range from 1 to 10 and an NA value (i.e. missing data). If we now would apply the do.call and sum functions as in Example 1, the output would be NA:

do.call("sum", list(x2))                      # Basic application of do.call
# NA

Fortunately, the sum function takes an argument called na.rm. If we want to specify this argument within the sum function, we can simply add our specification to the list of arguments:

do.call("sum", list(x2, na.rm = TRUE))        # Specify additional arguments
# 55

The result is 55 again – great!

 

Example 3: call() Function in R

A related function to the do.call function is the call function. Example 3 shows how to apply the call function in R.

Let’s assume that we want to compute the sum of our example vector x2 with the call function. Then, we can apply the call function as follows:

my_call <- call("sum", x2, na.rm = TRUE)      # Applying call function
my_call                                       # Return output of call function
# sum(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, NA), na.rm = TRUE)

The previous R syntax created a data object with the call mode. Now, we can use the eval function to evaluate this call object:

eval(my_call)                                 # Apply eval function to call object
# 55

55 again!

 

Video, Further Resources & Summary

Do you need further info on the R codes of this article? Then you may have a look at the following video tutorial of my YouTube channel. In the video, I’m explaining the R programming code of this article.

 

The YouTube video will be added soon.

 

This tutorial has explained how to use the do.call and call functions in simplified examples. Note that these functions are especially powerful when used in combination with more complex R codes such as in for-loops, while-loops, or user-defined functions. For that reason it makes a lot of sense to include those functions into your repertoire of R programming tricks!

Below, you can find a list of other articles on my website, in which the do.call and call functions are shown in more complex settings:

 

To summarize: At this point you should know how to call a function by its name in R. In case you have any additional questions, let me know in the comments. Furthermore, don’t forget to subscribe to my email newsletter in order to get regular updates on new tutorials.

 



Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top