Apply list of functions to input
Arguments
- ...
Named or unnamed arguments, each of which is a function taking exactly one input. See details.
- stringsAsFactors, check.names
Arguments of
data.frame
.
Value
Function with a single input which outputs a data.frame. Has special 'flist' entry in its environment which stores individual functions as list.
Details
This is a convenience function which takes a number of functions and returns
another function which applies all of the user specified functions to a new
input, and collects the results as list or data.frame.
This is useful to e.g. transform columns of a data.frame or check
the validity of a matrix during simulations. See the example here and
in simulate_data_conditional
.
The assumptions for the individual functions are:
Each function is expected to take a single input.
Each function is expected to output a result consistent with the other functions (i.e. same output length) to ensure that the results can be summarized as a data.frame.
Note
This function works fine without naming the input arguments, but the resulting data.frames have empty column names if that is the case. Thus, it is recommended to only pass named function arguments.
Examples
f <- function_list(
v1 = function(x) x[, 1] * 2,
v2 = function(x) x[, 2] + 10)
f(diag(2))
#> v1 v2
#> 1 2 10
#> 2 0 11
# function_list can be used to add new columns
# naming of columns should be handled separately in such cases
f <- function_list(
function(x) x, # return x as it is
X1_X2 = function(x) x[, 2] + 10) # add new column
f(diag(2))
#> X1 X2 X1_X2
#> 1 1 0 10
#> 2 0 1 11