Open In App

How to Use do.call in R

Last Updated : 01 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In R Programming language the do.call() function is used to execute a function call using a list of arguments. This can be particularly useful when we have a function and its arguments stored in separate objects (e.g., in a list) and we want to apply the function to these arguments.

Syntax

do.call(fun, args,quote = FALSE)

Where:

  • fun is the function you want to call.
  • args is a list or a pairlist containing the arguments to be passed to the function.
  • quote is a logical value indicating whether to evaluate the arguments before calling the function (FALSE by default).

Use do.call() with sum

In this example, we have a list values_list containing three vectors. We use do.call() to apply the sum() function to the values in the list, resulting in the sum of all values.

R




# Create a list of values
values <- list(A = c(7, 4, 6), B = c(9, 5, 10), C = c(3,3, 2))
values
# Calculate the sum of values in the list
do.call(sum, values)


Output:

$A
[1] 7 4 6
$B
[1] 9 5 10
$C
[1] 3 3 2
[1] 49

Use do.call() with mean

In this example, we define a list args containing two elements: a numeric vector 1:20 and a logical value na.rm = TRUE. By using do.call(mean, args), we execute the mean() function with the arguments specified in the list, effectively calculating the mean of the numeric vector while removing any NA values due to the na.rm = TRUE argument.

R




#define argument to use in do.call
args <- list(1:20, na.rm=TRUE)
args
#calculate mean of values in list
do.call(mean, args)


Output:

[[1]]
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
$na.rm
[1] TRUE
[1] 10.5

Use do.call() with rbind

In this example, we create three data frames df1, df2, and df3, each representing individuals with an ID and a name. We then use do.call() with the rbind() function to row-bind these data frames into a single data frame.

R




# Create three data frames
df1 <- data.frame(ID = 1:3, Name = c("Alice", "Bob", "Charlie"))
df2 <- data.frame(ID = 4:6, Name = c("David", "Emma", "Frank"))
df3 <- data.frame(ID = 7:9, Name = c("George", "Hannah", "Isaac"))
#place three data frames into list
df_list <- list(df1, df2, df3)
df_list
#row bind together all three data frames
do.call(rbind, df_list)


Output:

[[1]]
ID Name
1 1 Alice
2 2 Bob
3 3 Charlie
[[2]]
ID Name
1 4 David
2 5 Emma
3 6 Frank
[[3]]
ID Name
1 7 George
2 8 Hannah
3 9 Isaac
ID Name
1 1 Alice
2 2 Bob
3 3 Charlie
4 4 David
5 5 Emma
6 6 Frank
7 7 George
8 8 Hannah
9 9 Isaac

Using do.call() with paste

In this example, we have a list strings_list containing several strings. We use do.call() to apply the paste() function to concatenate these strings into a single sentence, specifying collapse = ” ” to separate them with spaces.

R




# Create a list of strings
strings_list <- list("Hello", "world!", "How", "are", "you?")
strings_list
# Combine strings in the list into a single sentence
do.call(paste, c(strings_list, collapse = " "))


Output:

[[1]]
[1] "Hello"
[[2]]
[1] "world!"
[[3]]
[1] "How"
[[4]]
[1] "are"
[[5]]
[1] "you?"
[1] "Hello world! How are you?"

Using do.call() with max

In this example, we have a list numbers_list containing two numeric vectors.By using do.call(max, numbers_list) to execute the max() function with the arguments specified in the list. This finds the maximum value in each numeric vector within numbers_list.

R




# Define a list of numeric vectors
numbers_list <- list(c(3, 6, 8, 12, 5), c(9, 4, 7, 2, 10))
numbers_list
# Find the maximum value in each vector
do.call(max, numbers_list)


Output:

[[1]]
[1] 3 6 8 12 5
[[2]]
[1] 9 4 7 2 10
[1] 12

Conclusion

In this article we get to know that the do.call() function serves as a versatile tool for executing function calls with arguments stored in lists. It streamlines the process of applying functions to multiple arguments, enhancing code flexibility and efficiency.

  • Through various examples, we’ve understood how do.call() can be used to perform diverse tasks, such as finding the maximum value in a list of vectors, concatenating strings, and calculating the mean of numeric vectors while handling missing values.
  • By understanding and leveraging do.call(), R users can simplify their code, improve workflow efficiency, and perform complex operations with ease. Its ability to dynamically execute function calls based on lists of arguments makes it a valuable asset in data manipulation, analysis, and beyond.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads