Description Usage Arguments Value See Also Examples
appplyBy
is an S3 generic function that applies a given function
to sub-matrices of a matrix-like object, which are generated according to
a factor that defines groups rows or columns.
In essence, applyBy.matrix
is a wrapper around colAvgsPerRowSet
and rowAvgsPerColSet
from the matrixStats
package,
which makes the computation really fast, but requires somehow cumbersome matrix arguments
to specify the groups of columns or rows.
The wrapper function builds the required arguments for cases where the groups
are defined by a factor or a list of indexes.
A method is provided for ExpressionSet
objects,
which preserve sample and feature annotations.
Moreover it allows directly passing names of feature/sample annotation – factor – variables
in argument BY
(see examples).
rowApplyBy
applies a function to rows of sub-matrices whose columns
are defined by a factor.
rowApplyBy
applies a function to columns of sub-matrices whose rows
are defined by a factor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | applyBy(x, ...)
## S3 method for class 'matrix'
applyBy(x, BY, MARGIN, FUN, W = NULL, ..., DROP = FALSE,
DUPS = NULL)
## S3 method for class 'ExpressionSet'
applyBy(x, BY, MARGIN, ..., ANNOTATIONS = TRUE)
## S3 method for class 'numeric'
applyBy(x, BY, MARGIN, ...)
rowApplyBy(x, BY, FUN, ...)
colApplyBy(x, BY, FUN, ...)
|
x |
matrix-like object on which |
... |
extra parameters passed to |
BY |
factor or object coerced to a factor, that defines the groups within
which the function If |
MARGIN |
margin along which the function |
FUN |
function to apply to each sub-matrix that contains the rows/columns
defined by each level of argument |
W |
An optional |
DROP |
logical that indicates if absent levels should be removed from the result matrix, or appear as 0-filled rows/columns. If |
DUPS |
logical, used when |
ANNOTATIONS |
logical that indicates if samples/feature annotations should
be kept, when the input data is an
In any case, the value of slot |
The result is a matrix or an ExpressionSet
object
whose margin's dimension MARGIN
is equal the same margin's
dimension in x
, and the other to the number of levels
in BY
.
built-ins
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 45 46 47 48 49 50 51 52 53 54 55 | # random data matrix
x <- matrix(runif(12 * 6), 12, 6)
# by groups of columns
fc <- gl(2, 3)
b <- applyBy(x, fc, 1L, rowSums)
b
# or
balt <- rowApplyBy(x, fc, rowSums)
stopifnot(identical(b, balt))
# by groups of rows
fr <- gl(3, 4)
b <- applyBy(x, fr, 2L, colSums)
# or
balt <- colApplyBy(x, fr, colSums)
stopifnot(identical(b, balt))
## Method for apply directly to ExpressionSet objects
library(Biobase)
x <- ExpressionSet(x, annotation='abcd.db')
y <- rowMinsBy(x, fc)
y <- colMinsBy(x, fr)
## annotations are conserved/collapsed
pData(x) <- data.frame(Group=fc, Sample=letters[1:ncol(x)])
pData(x)
fData(x) <- data.frame(ENTREZID=fr, Gene=letters[nrow(x):1])
fData(x)
# keep feature annotations, collapse sample annotations
y <- rowMinsBy(x, 'Group')
pData(y)
fData(y)
# keep sample annotations, collapse feature annotations
y <- colMinsBy(x, 'ENTREZID')
pData(y)
fData(y)
x <- runif(12)
# 3 groups of 4 elements
g <- gl(3, 4)
g
# row means ~> row matrix with 3 columns
a <- rowMeansBy(x, g)
a
# col means ~> column matrix with 3 rows
b <- colMeansBy(x, g)
b
# values are identical
stopifnot( identical(as.numeric(a), as.numeric(b)) )
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.