R: Recursively Apply a Function to a List of Data Objects

rapply2d {collapse} | R Documentation

## Recursively Apply a Function to a List of Data Objects

### Description

`rapply2d` is a recursive version of `lapply` with three differences to `rapply`:

1. data frames (or other list-based objects specified in `classes`) are considered as atomic, not as (sub-)lists

2. `FUN` is applied to all 'atomic' objects in the nested list

3. the result is not simplified / unlisted.

### Usage

    rapply2d(l, FUN, ..., classes = "data.frame")

### Arguments

`l` | a list.
`FUN` | a function that can be applied to all 'atomic' elements in l.
`...` | additional elements passed to FUN.
`classes` | character. Classes of list-based objects inside `l` that should be considered as atomic.

### Value

A list of the same structure as `l`, where `FUN` was applied to all atomic elements and list-based objects of a class included in `classes`.

### Note

The main reason `rapply2d` exists is to have a recursive function that out-of-the-box applies a function to a nested list of data frames.

For most other purposes `rapply`, or by extension the excellent rrapply function / package, provide more advanced functionality and greater performance.

### See Also

`rsplit`, `unlist2d`, List Processing, Collapse Overview

### Examples

    l <- list(mtcars, list(mtcars, as.matrix(mtcars)))
    rapply2d(l, fmean)
    unlist2d(rapply2d(l, fmean))

[Package collapse version 1.8.6 Index]