Movatterモバイル変換


[0]ホーム

URL:


rdrr.io

rapply: Recursively Apply a Function to a List

rapplyR Documentation

Recursively Apply a Function to a List

Description

rapply is a recursive version oflapply withflexibility inhow the result is structured (how = "..").

Usage

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

Arguments

object

alist orexpression, i.e., “list-like”.

f

afunction of one “principal” argument,passing further arguments via....

classes

character vector ofclass names, or"ANY" to match any class.

deflt

The default result (not used ifhow = "replace").

how

character string partially matching the three possibilities given:see ‘Details’.

...

additional arguments passed to the call tof.

Details

This function has two basic modes. Ifhow = "replace", eachelement ofobject which is not itself list-like and has a classincluded inclasses is replaced by the result of applyingf to the element.

Otherwise, with modehow = "list" orhow = "unlist",conceptuallyobjectis copied, all non-list elements which have a class included inclasses are replaced by the result of applyingf to theelement and all others are replaced bydeflt. Finally, ifhow = "unlist",unlist(recursive = TRUE) is called onthe result.

The semantics differ in detail fromlapply: inparticular the arguments are evaluated before calling the C code.

InR 3.5.x and earlier,object was required to be a list,which wasnot the case for its list-like components.

Value

Ifhow = "unlist", a vector, otherwise “list-like”of similar structure asobject.

References

Chambers, J. A. (1998)Programming with Data.Springer.
(rapply is only described briefly there.)

See Also

lapply,dendrapply.

Examples

X <- list(list(a = pi, b = list(c = 1L)), d = "a test")# the "identity operation":rapply(X, function(x) x, how = "replace") -> X.; stopifnot(identical(X, X.))rapply(X, sqrt, classes = "numeric", how = "replace")rapply(X, deparse, control = "all") # passing extras. argument of deparse()rapply(X, nchar, classes = "character", deflt = NA_integer_, how = "list")rapply(X, nchar, classes = "character", deflt = NA_integer_, how = "unlist")rapply(X, nchar, classes = "character",                      how = "unlist")rapply(X, log, classes = "numeric", how = "replace", base = 2)## with expression() / list():E  <- expression(list(a = pi, b = expression(c = C1 * C2)), d = "a test")LE <- list(expression(a = pi, b = expression(c = C1 * C2)), d = "a test")rapply(E, nchar, how="replace") # "expression(c = C1 * C2)" are 23 charsrapply(E, nchar, classes = "character", deflt = NA_integer_, how = "unlist")rapply(LE, as.character) # a "pi" | b1 "expression" | b2 "C1 * C2" ..rapply(LE, nchar)        # (see above)stopifnot(exprs = {  identical(E , rapply(E , identity, how = "replace"))  identical(LE, rapply(LE, identity, how = "replace"))})

What can we improve?

R Package Documentation

Browse R Packages

We want your feedback!

Note that we can't provide technical support on individual packages. You should contact the package authors for that.

 
Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, readEmbedding Snippets.

Close

[8]ページ先頭

©2009-2026 Movatter.jp