rapply
Apply a Function Recursively

Description

Applies the specified function recursively to all the leaf elements of the list object, to all leaf elements of nested list elements, and so on.

Usage

rapply(object, f, classes = "ANY", deflt = NULL,
       how = c("unlist", "replace", "list"), ...)

Arguments

object the object to which to apply the function recursively.
f the function to apply to leaf elements. The function must be a function object: it cannot be a character string giving the name of a function.
classes the classes in the object to which to apply the function. By default, "ANY", which matches objects of any class.
deflt a value inserted into the output when a leaf object does not match the classes argument, instead of inserting the result of calling f.

This value is not used when how matches "replace", because the old leaf object is inserted instead.

how controls the results. how should match the initial substring of one of the strings c("unlist", "replace", "list"). By default, it is "unlist". See the DETAILS section for more information.
... additional arguments passed to the function f.

Details

rapply scans through the elements of the list object, recursively scans through any list elements, and so on. It does not recursively scan through all types of recursive objects, but only ones where typeof(x)=="list". Any other object is considered a leaf object.
Value
returns a result determined by f and how. See the DETAILS section for more information.
See Also
apply, lapply, sapply.
Examples
x <- list(x=4, y=list(z=5, w='a'))
f <- function(x, y) x + y
rapply(x, f, classes='numeric', how='list', y=2)
# $x
# [1] 6
# 
# $y
# $y$z
# [1] 7
# 
# $y$w
# NULL

rapply(x, f, classes='numeric', how='unlist', y=2) # x y.z # 6 7

rapply(x, f, classes='numeric', deflt='test', how='list', y=2) # $x # [1] 6 # # $y # $y$z # [1] 7 # # $y$w # [1] "test"

rapply(x, f, classes='numeric', how='replace', y=2) # $x # [1] 6 # # $y # $y$z # [1] 7 # # $y$w # [1] "a"

Package base version 6.1.4-13
Package Index