List-utils: Common operations on List objects

Description Usage Arguments Details Value Author(s) See Also Examples

Description

Various functions and methods for looping on List objects, functional programming on List objects, and evaluation of an expression in a List object.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
## 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, ...)

revElements(x, i)

mendoapply(FUN, ..., MoreArgs=NULL)

pc(...)

## 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, ...)

## Constructing list matrices:
## ---------------------------------------------

## S4 method for signature 'List'
rbind(..., deparse.level=1L)
## S4 method for signature 'List'
cbind(..., deparse.level=1L)

Arguments

X, x

A list, data.frame or List object.

FUN

The function to be applied to each element of X (for endoapply) or for the elements in ... (for mendoapply).

...

For lapply, sapply, and endoapply, optional arguments to FUN.

For mendoapply, pc and Map, one or more list-like objects.

simplify, USE.NAMES

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

MoreArgs

A list of other arguments to FUN.

i

Index specifying the elements to replace. Can be anything supported by `[<-`.

f, init, right, accumulate, nomatch

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

data

A List object.

expr

Expression to evaluate.

deparse.level

See ?base::rbind for a description of this argument.

Details

Looping on List objects

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 an ordinary list.

revElements(x, i) reverses the list elements in x specified by i. It's equivalent to, but faster than, doing x[i] <- endoapply(x[i], rev).

pc(...) combine list-like objects by concatenating them in an element-wise fashion. It's similar to, but faster than, mapply(c, ..., SIMPLIFY=FALSE). With the following differences:

  1. pc() ignores the supplied objects that are NULL.

  2. pc() does not recycle its arguments. All the supplied objects must have the same length.

  3. If one of the supplied objects is a List object, then pc() returns a List object.

  4. pc() always returns a homogenous list or List object, that is, an object where all the list elements have the same type.

Functional programming methods for List objects

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.

Evaluation of an expression in 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.

Binding Lists into a matrix

There are methods for cbind and rbind that will bind multiple lists together into a basic list matrix. The usual geometric constraints apply. In the future, this might return a List (+ dimensions), but for now the return value is an ordinary list.

Value

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 ....

pc returns a list or List object of the same length as the input objects.

See ?base::Reduce for the value returned by the functional programming methods.

See ?base::within for the value returned by within.

cbind and rbind return a list matrix.

Author(s)

P. Aboyoun and H. Pagès

See Also

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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)

x <- list(a=11:13, b=26:21, c=letters)
y <- list(-(5:1), c("foo", "bar"), 0.25)
pc(x, y)

library(IRanges)
x <- IntegerList(a=11:13, b=26:21, c=31:36, d=4:2)
y <- NumericList(-(5:1), 1:2, numeric(0), 0.25)
pc(x, y)

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)

rbind(x, y)
cbind(x, y)

Example output

Loading required package: stats4
Loading required package: BiocGenerics
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, sd, var, xtabs

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

    Filter, Find, Map, Position, Reduce, anyDuplicated, append,
    as.data.frame, basename, cbind, colMeans, colSums, colnames,
    dirname, 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,
    rowMeans, rowSums, rownames, sapply, setdiff, sort, table, tapply,
    union, unique, unsplit, which, which.max, which.min


Attaching package: 'S4Vectors'

The following object is masked from 'package:base':

    expand.grid

            x           y
1  -1.4863011 -0.10372673
2  -1.1560120 -0.16578571
3  -0.8257228 -0.84369105
4  -0.4954337 -0.66719811
5  -0.1651446  2.69385670
6   0.1651446 -0.29587611
7   0.4954337 -0.44107437
8   0.8257228 -0.42229739
9   1.1560120 -0.04635587
10  1.4863011  0.29214864
       x           y
1  20.25 -0.07185992
2  12.25 -0.20922622
3   6.25  0.17981586
4   2.25  1.14220876
5   0.25  4.36738389
6   0.25  0.16341991
7   2.25 -0.18413890
8   6.25 -0.04536818
9  12.25  0.01334753
10 20.25 -0.39019882
$a
[1] "11" "12" "13" "-5" "-4" "-3" "-2" "-1"

$b
[1] "26"  "25"  "24"  "23"  "22"  "21"  "foo" "bar"

$c
 [1] "a"    "b"    "c"    "d"    "e"    "f"    "g"    "h"    "i"    "j"   
[11] "k"    "l"    "m"    "n"    "o"    "p"    "q"    "r"    "s"    "t"   
[21] "u"    "v"    "w"    "x"    "y"    "z"    "0.25"

NumericList of length 4
[["a"]] 11 12 13 -5 -4 -3 -2 -1
[["b"]] 26 25 24 23 22 21 1 2
[["c"]] 31 32 33 34 35 36
[["d"]] 4 3 2 0.25
[1] 72 72 72 72 72 72
IntegerList of length 2
[["b"]] 26 25 24 23 22 21
[["d"]] 4 3 2
$a
[1]    11    12    13 11000 12000 13000

$b
 [1]    26    25    24    23    22    21 26000 25000 24000 23000 22000 21000

$c
 [1]    31    32    33    34    35    36 31000 32000 33000 34000 35000 36000

$d
[1]    4    3    2 4000 3000 2000

  a         b         c         d        
x Integer,3 Integer,6 Integer,6 Integer,3
y Integer,3 Integer,6 Integer,6 Integer,3
  x         y        
a Integer,3 Integer,3
b Integer,6 Integer,6
c Integer,6 Integer,6
d Integer,3 Integer,3

S4Vectors documentation built on Dec. 11, 2020, 2:02 a.m.