| List-utils {S4Vectors} | R Documentation |
Various functions and methods for looping on List objects, functional programming on List objects, and evaluation of an expression in a List object.
## Looping on List objects: ## ------------------------ ## S4 method for signature 'List' lapply(X, FUN, ...) ## S4 method for signature 'List' sapply(X, FUN, ..., simplify=TRUE, USE.NAMES=TRUE) endoapply(X, FUN, ...) mendoapply(FUN, ..., MoreArgs=NULL) revElements(x, i) ## Functional programming methods for List objects: ## ------------------------------------------------ ## S4 method for signature 'List' Reduce(f, x, init, right=FALSE, accumulate=FALSE) ## S4 method for signature 'List' Filter(f, x) ## S4 method for signature 'List' Find(f, x, right=FALSE, nomatch=NULL) ## S4 method for signature 'List' Map(f, ...) ## S4 method for signature 'List' Position(f, x, right=FALSE, nomatch=NA_integer_) ## Evaluation of an expression in a List object: ## --------------------------------------------- ## S4 method for signature 'List' within(data, expr, ...)
X, x |
A list, data.frame or List object. |
FUN |
The function to be applied to each element of |
... |
For For For |
simplify, USE.NAMES |
See |
MoreArgs |
A list of other arguments to |
i |
Index specifying the elements to replace. Can be anything supported
by |
f, init, right, accumulate, nomatch |
See |
data |
A List object. |
expr |
Expression to evaluate. |
Like the standard lapply function defined in the
base package, the lapply method for List objects
returns a list of the same length as X, with each element being
the result of applying FUN to the corresponding element of X.
Like the standard sapply function defined in the
base package, the sapply method for List objects
is a user-friendly version of lapply by default returning a vector
or matrix if appropriate.
endoapply and mendoapply perform the endomorphic equivalents
of lapply and mapply by returning
objects of the same class as the inputs rather than a list.
revElements is a convenient way to do
x[i] <- endoapply(x[i], rev).
The R base package defines some higher-order functions that are commonly
found in Functional Programming Languages.
See ?base::Reduce for the details, and, in particular,
for a description of their arguments.
The S4Vectors package provides methods for List objects, so,
in addition to be an ordinary vector or list, the x argument can
also be a List object.
within evaluates expr within as.env(data) via
eval(data). Similar to with, except assignments made
during evaluation are taken as assignments into data, i.e.,
new symbols have their value appended to data, and assigning
new values to existing symbols results in replacement.
endoapply returns an object of the same class as X,
each element of which is the result of applying FUN to the
corresponding element of X.
mendoapply returns an object of the same class as the first
object specified in ..., each element of which is the result
of applying FUN to the corresponding elements of ....
See ?base::Reduce for the value returned by the
functional programming methods.
See ?base::within for the value returned by
within.
P. Aboyoun
The List class.
base::lapply and base::mapply
for the default lapply and mapply methods.
base::Reduce for the default functional
programming methods.
base::within for the default within
method.
a <- data.frame(x = 1:10, y = rnorm(10))
b <- data.frame(x = 1:10, y = rnorm(10))
endoapply(a, function(x) (x - mean(x))/sd(x))
mendoapply(function(e1, e2) (e1 - mean(e1)) * (e2 - mean(e2)), a, b)
library(IRanges)
x <- IntegerList(a=1:3, b=16:11, c=22:21, d=31:36)
x
Reduce("+", x)
Filter(is.unsorted, x)
pos1 <- Position(is.unsorted, x)
stopifnot(identical(Find(is.unsorted, x), x[[pos1]]))
pos2 <- Position(is.unsorted, x, right=TRUE)
stopifnot(identical(Find(is.unsorted, x, right=TRUE), x[[pos2]]))
y <- x * 1000L
Map("c", x, y)