R do.call使用语法

2020-05-15

do.call constructs and executes a function call from a name or a function and a list of arguments to be passed to it.

Usage

do.call(what, args, quote = FALSE, envir = parent.frame())

Arguments

what either a function or a non-empty character string naming the function to be called.
args a list of arguments to the function call. The names attribute of args gives the argument names.
quote a logical value indicating whether to quote the arguments.
envir an environment within which to evaluate the call. This will be most useful if what is a character string and the arguments are symbols or quoted expressions.

最常用的就是,使用do.call将一个list 中n个数据框,通过rbind或者cbind 的方式合并成一个数据框。即起到了转化list数据结构为dataframe数据结构的作用。下面为Stack Overflow的一个与lapply, map比较的介绍,以及一些例子,讲述的十分清楚。

do.call 和lapply 的区别

answer 1

There is a function called Map that may be similar to map in other languages:

  • lapply returns a list of the same length as X, each element of which is the result of applying FUN to the corresponding element of X.
  • do.call constructs and executes a function call from a name or a function and a list of arguments to be passed to it.
  • Map applies a function to the corresponding elements of given vectors… Map is a simple wrapper to mapply which does not attempt to simplify the result, similar to Common Lisp’s mapcar (with arguments being recycled, however). Future versions may allow some control of the result type.

  1. Map is a wrapper around mapply
  2. lapply is a special case of mapply
  3. Therefore Map and lapply will be similar in many cases.

For example, here is lapply:

lapply(iris, class)
$Sepal.Length
[1] "numeric"

$Sepal.Width
[1] "numeric"

$Petal.Length
[1] "numeric"

$Petal.Width
[1] "numeric"

$Species
[1] "factor"

And the same using Map:

Map(class, iris)
$Sepal.Length
[1] "numeric"

$Sepal.Width
[1] "numeric"

$Petal.Length
[1] "numeric"

$Petal.Width
[1] "numeric"

$Species
[1] "factor"

do.call takes a function as input and splatters its other arguments to the function. It is widely used, for example, to assemble lists into simpler structures (often with rbind or cbind).

For example:

x <- lapply(iris, class)
do.call(c, x)
Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
   "numeric"    "numeric"    "numeric"    "numeric"     "factor" 

answer 2

lapply applies a function over a list, do.call calls a function with a list of arguments.

lapply is similar to map, do.call is not. lapply applies a function to all elements of a list, do.call calls a function where all the function arguments are in a list. So for a n element list, lapply has n function calls, and do.call has just one function call. So do.call is quite different from lapply. Hope this clarifies your issue.

A code example:

do.call(sum, list(c(1, 2, 4, 1, 2), na.rm = TRUE))
[1] 10

and:

lapply(c(1, 2, 4, 1, 2), function(x) x + 1)
[[1]]
[1] 2

[[2]]
[1] 3

[[3]]
[1] 5

[[4]]
[1] 2

[[5]]
[1] 3

answer 3

In most simple words:

  1. lapply() applies a given function for each element in a list,so there will be several function calls.
  2. do.call() applies a given function to the list as a whole,so there is only one function call.