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.
Similar Reads
How To Use A For Loop In R
For loops in R is a fundamental programming construct that allows you to repeat a block of code a specified number of times or for a given range of elements. They are essential for automating repetitive tasks, manipulating data, and performing various computational operations. The basic syntax of a
3 min read
How to Fix do.call Error in R
In R Programming Language do. call is a powerful function that allows you to call another function with a list of arguments. However, it can sometimes throw error messages. Addressing these errors requires a comprehensive understanding of common effective strategies for solutions. In this article, w
3 min read
How to Use min() and max() in R
In this article, we will discuss Min and Max functions in R Programming Language. Min: The Min function is used to return the minimum value in a vector or the data frame. Syntax: In a vector: min(vector_name) In a dataframe with in a column: min(dataframe$column_name) In a dataframe multiple columns
3 min read
How to Use sum Function in R?
In this article, we will discuss how to use the sum() function in the R Programming Language. sum() function: This is used to return the total/sum of the given data Syntax: sum(data) Arguments: data can be a vector or a dataframeExample 1: Using sum() function to calculate the sum of vector elements
5 min read
How to Use setwd and getwd in R?
In this article, we will discuss how to use setwd and getwd in the R programming language. getwd() function getwd() stands forget working directory. It is used to get the current working directory of the environment. Syntax: getwd() We can also see the total number of files in the present working di
1 min read
How to Use Nrow Function in R?
In this article, we will discuss how to use Nrow function in R Programming Language. This function is used in the dataframe or the matrix to get the number of rows. Syntax: nrow(data) where, data can be a dataframe or a matrix. Example 1: Count Rows in Data Frame In this example, we are going to cou
2 min read
How to Use na.rm in R?
In this article, we will discuss how to use na.rm in R Programming Language. na.rm in R is used to remove the NA values. na.rm in vector When we perform any operation, we have to exclude NA values, otherwise, the result would be NA. Syntax: function(vector,na.rm) where vector is input vectorna.rm is
3 min read
How to Use file.path() Function in R
R programming language is becoming popular among developers, analysts, and mainly for data scientists. Students are eagerly learning R with Python language to use their analytical skills at their best. While learning any language, one is faced with many difficulties, and the individual learning R Pr
3 min read
How to Deal with Unlist Error in R
In this article, we will discuss the "object not found" error as a frequent challenge, particularly in conjunction with the unlist function, and try to solve those errors in R Programming Language. Understanding the unlist ErrorThe "object not found" error in R surfaces when the interpreter encounte
3 min read
How to Debug class Error in R
In R Programming language Debugging is an essential skill for any programmer, and R developers are no exception. One common challenge that R programmers face is dealing with class errors, where the expected class of an object does not match the actual class encountered during runtime. Table of Conte
4 min read