 # call In R: The do.call() and call() function In this article, we will learn about the do.call() function and call() function in R programming language together. Let’s go into detail now.

## What is the do.call() and call() function in R?

The do.call() and call() function create an object but the do.call() function executes a function using its name and the specified arguments, and the call() function is not.

Syntax:

do.call(func, list)
call(func, list)

Parameters:

• funct: This identifies the Function to be called and either represents the Function, …
• list: The list of parameters to the function call.

## How to use do.call() and call() function in R?

### The example of do.call() function

Here, we will use the do.call() function with basic functions. First, we will create data for this example as follows:

# Create a data
data <- c(1:10) * 3

# View a data
data

Output

   3  6  9 12 15 18 21 24 27 30


We need to use the sum function to determine the vector’s sum. Let’s further assume that the sum function’s Name, sum, is the only thing we have recorded as a character string. The sum function can then be run using the do.call command as seen below:

# Create a data
data <- c(1:10) * 3

# Print the sum of the data
res <- do.call("sum",list(data))
cat("The sum is:",res)

Output

The sum is: 165

Similarly, we can row bind (rbind) data frame columns. See the code example:

# Create a data frame
data1 <- data.frame(
Math = c(9,8,6),
English = c(8,6,9)
)

data2 <- data.frame(
Math = c(5,6,9),
English = c(8,7,6)
)

data3 <- data.frame(
Math = c(9,8,6),
English = c(3,7,6)
)

# The row bind column
cat("The row bind is:\n")
do.call(rbind,list(data1,data2,data3))

Output

The row bind is:
Math English
1    9       8
2    8       6
3    6       9
4    5       8
5    6       7
6    9       6
7    9       3
8    8       7
9    6       6

### The example of call() function

Assume that we want to use the call function to sum the elements of our sample vector data. Following that, we can use as follows:

# Create a data
data <- c(1:10) * 3

# Using call() function
res <- call("sum", data)
res # sum(c(3, 6, 9, 12, 15, 18, 21, 24, 27, 30))

So, we will use the eval function to calculate the sum of this vector.

# Create a data
data <- c(1:10) * 3

# Using call() function
res <- call("sum", data)
res

# Calculate the sum and print
cat("The sum is:",eval(res))

Output

sum(c(3, 6, 9, 12, 15, 18, 21, 24, 27, 30))
The sum is: 165

Another example, we can round numbers by using the call() function.

# Create a data
data <- 3.2

# Round the data
res <- call("round",data) # round(3.2)
cat("The round number is:",eval(res))

Output

The round number is: 3