How to Use do.call in R with examples

How to Use do.call in R?. In R, you can use do.call() to apply a function to a whole list.

How to Use do.call in R

The following is the fundamental syntax for this function:

do.call(function, list)

The examples below demonstrate how to use do.call() in practice.

Naive Bayes Classifier in Machine Learning » Prediction Model » finnstats

Example 1: Use do.call() with sum

The following code demonstrates how to calculate the total of values in a list using do.call():

Now we can create a list

vlist <- list(A=c(8, 22,143), B=c(47, 75, 88), C=c(33, 15, 25))

Let’s calculate the minimum from the list

do.call(min, vlist)
[1] 8

The minimum value in the list is 48.

If we tried to use sum() directly on the list, we would get the following error.

Anderson-Darling Test in R (Quick Normality Check) » finnstats

vlist <- list(A=c(8, 22,143), B=c(47, 75, 88), C=c(33, 15, 25))

If we try min function directly following output will get.

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

Example 2: Use do.call() with mean

The following code demonstrates how to calculate the mean of a list of variables using do.call().

define the argument that will be used in do.call

listvalue<- list(1:50, na.rm=TRUE)

Now we can calculate the mean of values in the list

do.call(mean, listvalue)
[1] 25.5

The average of the list’s values is 25.5.

If we tried to use mean() directly on the list, we would get the following error:

mean(list(1:50), 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 demonstrates how to row link numerous data frames in R using do.call().

Goodness of Fit Test- Jarque-Bera Test in R » finnstats

Let’s make three data frames together.

df1 <- data.frame(Name=c('Q1', 'Q2', 'Q3'),
                  Score=c(44, 66, 77))
df2 <- data.frame(Name=c('P1', 'P2', 'P3'),
                  Score=c(88, 33, 55))
df3 <- data.frame(Name=c('Y1', 'Y2', 'Y3I'),
                  Score=c(81, 45, 58))

Now we can add data frames into list

dflist <- list(df1, df2, df3)

Okay, now ready to row bind together all three data frames

do.call(rbind, dflist)
Name Score
1   Q1    44
2   Q2    66
3   Q3    77
4   P1    88
5   P2    33
6   P3    55
7   Y1    81
8   Y2    45
9  Y3I    58

As a result, one data frame is created, which comprises the rows from all three data frames.

If we tried to use rbind() directly with the list, we would not get the correct data frame.

How to Perform a Lack of Fit Test in R-Quick Guide » finnstats

Suppose if we try to row bind together all three data frames

rbind(dflist)
[,1]   [,2]   [,3] 
dflist List,2 List,2 List,2

Hope you liked this article, please share and subscribe.

You may also like...

2 Responses

  1. Anonymous says:

    Great post!
    Please use https://rdrr.io/cran/BBmisc/man/do.call2.html to avoid a copy.

Leave a Reply

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

18 − 1 =