| rapply | R Documentation |
rapply is a recursive version oflapply withflexibility inhow the result is structured (how = "..").
rapply(object, f, classes = "ANY", deflt = NULL, how = c("unlist", "replace", "list"), ...)object | a |
f | a |
classes | character vector of |
deflt | The default result (not used if |
how | character string partially matching the three possibilities given:see ‘Details’. |
... | additional arguments passed to the call to |
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.
Ifhow = "unlist", a vector, otherwise “list-like”of similar structure asobject.
Chambers, J. A. (1998)Programming with Data.Springer.
(rapply is only described briefly there.)
lapply,dendrapply.
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"))})Add the following code to your website.
For more information on customizing the embed code, readEmbedding Snippets.