How to Use do.call in R (3 Examples)


You can use do.call() in R to apply a given function to a list as a whole.

This function uses the following basic syntax:

do.call(function, list)

The following examples show how to use do.call() in practice.

Example 1: Use do.call() with sum

The following code shows how to use do.call() to calculate the sum of values in a list:

#create list
values_list <- list(A=c(1, 2, 3), B=c(7, 5, 10), C=c(9, 9, 2))

#calculate sum of values in list
do.call(sum, values_list)

[1] 48

The sum of the values in the list is 48.

Note that we would receive an error if we just tried to use sum() directly with the list:

#create list
values_list <- list(A=c(1, 2, 3), B=c(7, 5, 10), C=c(9, 9, 2))

#attempt to sum values in list
sum(values_list)

Error in sum(values_list) : invalid 'type' (list) of argument

Example 2: Use do.call() with mean

The following code shows how to use do.call() to calculate the mean of values in a list:

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

#calculate mean of values in list
do.call(mean, args)

[1] 10.5

The mean of the values in the list is 10.5.

Note that we would receive an error if we just tried to use mean() directly with the list:

#attempt to calculate mean of values in list
mean(list(1:20), na.rm=TRUE)

[1] NA
Warning message:
In mean.default(list(1:20), na.rm = TRUE) :
  argument is not numeric or logical: returning NA

Example 3: Use do.call() with rbind

The following code shows how to use do.call() to row bind together several data frames in R:

#create three data frames
df1 <- data.frame(team=c('A', 'B', 'C'),
                  points=c(22, 27, 38))

df2 <- data.frame(team=c('D', 'E', 'F'),
                  points=c(22, 14, 20))

df3 <- data.frame(team=c('G', 'H', 'I'),
                  points=c(11, 15, 18))

#place three data frames into list
df_list <- list(df1, df2, df3)

#row bind together all three data frames
do.call(rbind, df_list)

  team points
1    A     22
2    B     27
3    C     38
4    D     22
5    E     14
6    F     20
7    G     11
8    H     15
9    I     18

The result is one data frame that contains the rows from each of the three data frames.

Note that we would not receive the desired data frame if we tried to use rbind() directly with the list:

#create three data frames
df1 <- data.frame(team=c('A', 'B', 'C'),
                  points=c(22, 27, 38))

df2 <- data.frame(team=c('D', 'E', 'F'),
                  points=c(22, 14, 20))

df3 <- data.frame(team=c('G', 'H', 'I'),
                  points=c(11, 15, 18))

#place three data frames into list
df_list <- list(df1, df2, df3)

#attmempt to row bind together all three data frames
rbind(df_list)

        [,1]   [,2]   [,3]  
df_list List,2 List,2 List,2

Additional Resources

The following tutorials explain how to use other common functions in R:

How to Use paste & paste0 Functions in R
How to Use the replace() Function in R
How to Use the View() Function in R
How to Use rep() Function in R

Leave a Reply

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