summarizeRows: Summarizing rows/columns by a factor

Description Usage Arguments Details Value Author(s) Examples

View source: R/summarizeRows.R

Description

Apply a function to summarize rows/columns that assigned to the same level by a factor vector.

Usage

1

Arguments

matrix

A numeric matrix

factor

A vector of factors, either of the length of nrow(matrix) (for summarizeRows), or the length of ncol(matrix) (for summarizeColumns).

fun

A function or a name for a function, the summarizing function applied to rows/columns sharing the same level

...

Further parameters passed to the function

Details

NA levels are neglected, and corresponding rows/columns will not contribute to the summarized matrix.

summarizeCols is synonymous to summarizeColumns

Value

A matrix, the dimension will be determined by the number of levels of the factor vector.

Author(s)

Jitao David Zhang <jitao_david.zhang@roche.com>

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
my.matrix <- matrix(1:25, nrow=5)
print(my.matrix)

my.factor <- factor(c("A", "B", "A", "C", "B"))
summarizeRows(matrix=my.matrix, factor=my.factor, fun=mean)
summarizeRows(matrix=my.matrix, factor=my.factor, fun=prod)
summarizeColumns(matrix=my.matrix, factor=my.factor, fun=mean)
summarizeColumns(matrix=my.matrix, factor=my.factor, fun=prod)

## NA values in factor
my.na.factor <- factor(c("A", "B", "A", "C", NA))
summarizeRows(matrix=my.matrix, factor=my.na.factor, fun=mean)
summarizeRows(matrix=my.matrix, factor=my.na.factor, fun=prod)
summarizeColumns(matrix=my.matrix, factor=my.na.factor, fun=mean)
summarizeColumns(matrix=my.matrix, factor=my.na.factor, fun=prod)

ribiosUtils documentation built on March 13, 2020, 2:54 a.m.