The R Project SVN R

Rev

Rev 68948 | Rev 88589 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 68948 Rev 74835
Line 1... Line 1...
1
% File src/library/base/man/rapply.Rd
1
% File src/library/base/man/rapply.Rd
2
% Part of the R package, https://www.R-project.org
2
% Part of the R package, https://www.R-project.org
3
% Copyright 1995-2007 R Core Team
3
% Copyright 1995-2018 R Core Team
4
% Distributed under GPL 2 or later
4
% Distributed under GPL 2 or later
5
 
5
 
6
\name{rapply}
6
\name{rapply}
7
\alias{rapply}
7
\alias{rapply}
8
\title{Recursively Apply a Function to a List}
8
\title{Recursively Apply a Function to a List}
9
\description{
9
\description{
10
  \code{rapply} is a recursive version of \code{\link{lapply}}.
10
  \code{rapply} is a recursive version of \code{\link{lapply}} with
-
 
11
  flexibility in \emph{how} the result is structured (\code{how = ".."}).
11
}
12
}
12
\usage{
13
\usage{
13
rapply(object, f, classes = "ANY", deflt = NULL,
14
rapply(object, f, classes = "ANY", deflt = NULL,
14
       how = c("unlist", "replace", "list"), ...)
15
       how = c("unlist", "replace", "list"), ...)
15
}
16
}
16
\arguments{
17
\arguments{
17
  \item{object}{A list.}
18
  \item{object}{a \code{\link{list}} or \code{\link{expression}}, i.e., \dQuote{list-like}.}
18
  \item{f}{A function of a single argument.}
19
  \item{f}{a \code{\link{function}} of one \dQuote{principal} argument,
-
 
20
    passing further arguments via \code{\dots}.}
19
  \item{classes}{A character vector of \code{\link{class}} names, or
21
  \item{classes}{character vector of \code{\link{class}} names, or
20
    \code{"ANY"} to match any class.}
22
    \code{"ANY"} to match any class.}
21
  \item{deflt}{The default result (not used if \code{how = "replace"}).}
23
  \item{deflt}{The default result (not used if \code{how = "replace"}).}
22
  \item{how}{A character string partially matching the three possibilities given:
24
  \item{how}{character string partially matching the three possibilities given:
23
    see \sQuote{Details}.}
25
    see \sQuote{Details}.}
24
  \item{\dots}{additional arguments passed to the call to \code{f}.}
26
  \item{\dots}{additional arguments passed to the call to \code{f}.}
25
}
27
}
26
\details{
28
\details{
27
  This function has two basic modes.  If \code{how = "replace"}, each
29
  This function has two basic modes.  If \code{how = "replace"}, each
28
  element of the list which is not itself a list and has a class
30
  element of \code{object} which is not itself list-like and has a class
29
  included in \code{classes} is replaced by the result of applying
31
  included in \code{classes} is replaced by the result of applying
30
  \code{f} to the element.
32
  \code{f} to the element.
31
 
33
 
32
  If the mode is \code{how = "list"} or \code{how = "unlist"}, the list
34
  Otherwise, with mode \code{how = "list"} or \code{how = "unlist"},
-
 
35
  conceptually \code{object}
33
  is copied, all non-list elements which have a class included in
36
  is copied, all non-list elements which have a class included in
34
  \code{classes} are replaced by the result of applying \code{f} to the
37
  \code{classes} are replaced by the result of applying \code{f} to the
35
  element and all others are replaced by \code{deflt}.  Finally, if
38
  element and all others are replaced by \code{deflt}.  Finally, if
36
  \code{how = "unlist"}, \code{unlist(recursive = TRUE)} is called on
39
  \code{how = "unlist"}, \code{unlist(recursive = TRUE)} is called on
37
  the result.
40
  the result.
38
 
41
 
39
  The semantics differ in detail from \code{\link{lapply}}: in
42
  The semantics differ in detail from \code{\link{lapply}}: in
40
  particular the arguments are evaluated before calling the C code.
43
  particular the arguments are evaluated before calling the C code.
-
 
44
 
-
 
45
  In \R 3.5.x and earlier, \code{object} was required to be a list,
-
 
46
  which was \emph{not} the case for its list-like components.
41
}
47
}
42
\value{
48
\value{
43
  If \code{how = "unlist"}, a vector, otherwise a list of similar
49
  If \code{how = "unlist"}, a vector, otherwise \dQuote{list-like}
44
  structure to \code{object}.
50
  of similar structure as \code{object}.
45
}
51
}
46
\seealso{
52
\seealso{
47
  \code{\link{lapply}}, \code{\link{dendrapply}}.
53
  \code{\link{lapply}}, \code{\link{dendrapply}}.
48
}
54
}
49
\references{
55
\references{
Line 51... Line 57...
51
  \emph{Programming with Data}.
57
  \emph{Programming with Data}.
52
  Springer.\cr
58
  Springer.\cr
53
  (\code{rapply} is only described briefly there.)
59
  (\code{rapply} is only described briefly there.)
54
}
60
}
55
\examples{
61
\examples{
56
X <- list(list(a = pi, b = list(c = 1:1)), d = "a test")
62
X <- list(list(a = pi, b = list(c = 1L)), d = "a test")
-
 
63
# the "identity operation":
57
rapply(X, function(x) x, how = "replace")
64
rapply(X, function(x) x, how = "replace") -> X.; stopifnot(identical(X, X.))
58
rapply(X, sqrt, classes = "numeric", how = "replace")
65
rapply(X, sqrt, classes = "numeric", how = "replace")
59
rapply(X, nchar, classes = "character",
66
rapply(X, deparse, control = "all") # passing extras. argument of deparse()
60
       deflt = as.integer(NA), how = "list")
-
 
61
rapply(X, nchar, classes = "character",
67
rapply(X, nchar, classes = "character", deflt = NA_integer_, how = "list")
62
       deflt = as.integer(NA), how = "unlist")
68
rapply(X, nchar, classes = "character", deflt = NA_integer_, how = "unlist")
63
rapply(X, nchar, classes = "character", how = "unlist")
69
rapply(X, nchar, classes = "character",                      how = "unlist")
64
rapply(X, log, classes = "numeric", how = "replace", base = 2)
70
rapply(X, log, classes = "numeric", how = "replace", base = 2)
-
 
71
 
-
 
72
## with expression() / list():
-
 
73
E  <- expression(list(a = pi, b = expression(c = C1 * C2)), d = "a test")
-
 
74
LE <- list(expression(a = pi, b = expression(c = C1 * C2)), d = "a test")
-
 
75
rapply(E, nchar, how="replace") # "expression(c = C1 * C2)" are 23 chars
-
 
76
rapply(E, nchar, classes = "character", deflt = NA_integer_, how = "unlist")
-
 
77
rapply(LE, as.character) # a "pi" | b1 "expression" | b2 "C1 * C2" ..
-
 
78
rapply(LE, nchar)        # (see above)
-
 
79
stopifnot(exprs = {
-
 
80
  identical(E , rapply(E , identity, how = "replace"))
-
 
81
  identical(LE, rapply(LE, identity, how = "replace"))
-
 
82
})
65
}
83
}
66
\keyword{iteration}
84
\keyword{iteration}
67
\keyword{list}
85
\keyword{list}