Learn R Programming

ribiosUtils (version 1.5-6)

summarizeRows: Summarizing rows/columns by a factor

Description

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

Usage

summarizeRows(matrix, factor, fun = mean, ...)

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

Value

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

Details

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

summarizeCols is synonymous to summarizeColumns

Examples

Run this code
# NOT RUN {
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)

# }

Run the code above in your browser using DataLab