R allows you to pass functions as arguments to functions. This means that you can define apply.func
as follows (where f
is a function, and ...
are all other parameters:
apply.func <- function(f, ...)f(...)
Then you can use apply.func
to and specify any function where the parameters make sense:
apply.func(paste, 1, 2, 3) [1] "1 2 3" apply.func(sum, 1, 2, 3) [1] 6
However, note that the following may not produce the expected results, since mean
takes a vector as an argument:
apply.func(mean, 1, 2, 3) [1] 1
Note that there is also a basic R function called do.call
that actually does the same thing.
Andrie
source share