Skip to contents

Recursively apply a function to a list or lists.

Usage

rMapply(FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE, USE.NAMES = TRUE)

Arguments

FUN

Function to apply.

...

Object(s) to which FUN can be applied, or lists of such objects to iterate over (defined narrowly, as of class "list").

MoreArgs

A list of additional arguments to FUN.

SIMPLIFY

Logical, whether to simplify the results to a vector or array.

USE.NAMES

Logical, whether to use the names of the first list object in ... for the output.

Value

The output of FUN in a list or nested list, or simplified to a vector or array (or list of arrays).

Details

rMapply() recursively applies FUN to the elements of the lists in ... via mapply(). If only a single list is supplied, the function acts like a recursive version of sapply(). The particular condition that determines if the function should stop recursing is if either the first or second objects in ... are not of class "list". Thus, unlike mapply(), it will not iterate over non-list elements in these objects, but instead returns the output of FUN(...).

This is primarily a convenience function used internally to enable recursive application of functions to lists or nested lists. Its particular stop condition for recursing is also designed to either a) act as a wrapper for FUN if the first object in ... is not a list, or b) apply a weighted averaging operation if the first object is a list and the second object is a numeric vector of weights.