lapply: Apply a function over a list-like or vector-like object

Description Usage Arguments Value See Also Examples

Description

lapply returns a list of the same length as X, each element of which is the result of applying FUN to the corresponding element of X.

sapply is a user-friendly version and wrapper of lapply by default returning a vector, matrix or, if simplify="array", an array if appropriate, by applying simplify2array(). sapply(x, f, simplify=FALSE, USE.NAMES=FALSE) is the same as lapply(x, f).

NOTE: This man page is for the lapply and sapply S4 generic functions defined in the BiocGenerics package. See ?base::lapply for the default methods (defined in the base package). Bioconductor packages can define specific methods for objects (typically list-like or vector-like) not supported by the default methods.

Usage

1
2
lapply(X, FUN, ...)
sapply(X, FUN, ..., simplify=TRUE, USE.NAMES=TRUE)

Arguments

X

A list-like or vector-like object.

FUN, ..., simplify, USE.NAMES

See ?base::lapply for a description of these arguments.

Value

See ?base::lapply for the value returned by the default methods.

Specific methods defined in Bioconductor packages should behave as consistently as possible with the default methods. In particular, lapply and sapply(simplify=FALSE) should always return a list.

See Also

Examples

1
2
3
4
5
6
7
lapply  # note the dispatch on the 'X' arg only
showMethods("lapply")
selectMethod("lapply", "ANY")  # the default method

sapply  # note the dispatch on the 'X' arg only
showMethods("sapply")
selectMethod("sapply", "ANY")  # the default method

Example output

Loading required package: parallel

Attaching package: 'BiocGenerics'

The following objects are masked from 'package:parallel':

    clusterApply, clusterApplyLB, clusterCall, clusterEvalQ,
    clusterExport, clusterMap, parApply, parCapply, parLapply,
    parLapplyLB, parRapply, parSapply, parSapplyLB

The following objects are masked from 'package:stats':

    IQR, mad, xtabs

The following objects are masked from 'package:base':

    Filter, Find, Map, Position, Reduce, anyDuplicated, append,
    as.data.frame, cbind, colnames, do.call, duplicated, eval, evalq,
    get, grep, grepl, intersect, is.unsorted, lapply, lengths, mapply,
    match, mget, order, paste, pmax, pmax.int, pmin, pmin.int, rank,
    rbind, rownames, sapply, setdiff, sort, table, tapply, union,
    unique, unsplit, which, which.max, which.min

standardGeneric for "lapply" defined from package "BiocGenerics"

function (X, FUN, ...) 
standardGeneric("lapply")
<environment: 0x32e51a8>
Methods may be defined for arguments: X
Use  showMethods("lapply")  for currently available ones.
Function: lapply (package BiocGenerics)
X="ANY"

Method Definition (Class "derivedDefaultMethod"):

function (X, FUN, ...) 
{
    FUN <- match.fun(FUN)
    if (!is.vector(X) || is.object(X)) 
        X <- as.list(X)
    .Internal(lapply(X, FUN))
}
<environment: namespace:base>

Signatures:
        X    
target  "ANY"
defined "ANY"
standardGeneric for "sapply" defined from package "BiocGenerics"

function (X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE) 
standardGeneric("sapply")
<environment: 0x30fd220>
Methods may be defined for arguments: X
Use  showMethods("sapply")  for currently available ones.
Function: sapply (package BiocGenerics)
X="ANY"

Method Definition (Class "derivedDefaultMethod"):

function (X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE) 
{
    FUN <- match.fun(FUN)
    answer <- lapply(X = X, FUN = FUN, ...)
    if (USE.NAMES && is.character(X) && is.null(names(answer))) 
        names(answer) <- X
    if (!identical(simplify, FALSE) && length(answer)) 
        simplify2array(answer, higher = (simplify == "array"))
    else answer
}
<environment: namespace:base>

Signatures:
        X    
target  "ANY"
defined "ANY"

BiocGenerics documentation built on Nov. 17, 2017, 1:23 p.m.