Learn R Programming

simdata (version 0.4.1)

function_list: Apply list of functions to input

Description

Apply list of functions to input

Usage

function_list(..., stringsAsFactors = FALSE, check.names = TRUE)

Value

Function with a single input which outputs a data.frame. Has special 'flist' entry in its environment which stores individual functions as list.

Arguments

...

Named or unnamed arguments, each of which is a function taking exactly one input. See details.

stringsAsFactors, check.names

Arguments of data.frame.

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.

See Also

data.frame, get_from_function_list, get_names_from_function_list

Examples

Run this code
f <- function_list(
    v1 = function(x) x[, 1] * 2,
    v2 = function(x) x[, 2] + 10)

f(diag(2))

# 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))

Run the code above in your browser using DataLab