applyByCategory {Category}R Documentation

Apply a function to a vector of statistics, by category

Description

For each category, apply the function FUN to the set of values of stats belonging to that category.

Usage

applyByCategory(stats, Amat, FUN = mean, ...)

Arguments

stats Numeric vector with test statistics of interest. If Amat is an object of class graphNEL, then stats needs to be named, with names corresponding to those in Amat.
Amat A data structure that codes category memberships of the elements of stats. It can be either a matrix or a graphNEL object. See details.
FUN A function to apply to the subsets stats by categories.
... Extra parameters passed to FUN.

Details

If Amat is an object of class graphNEL, the categories are taken to be those nodes of Amat whose names do not match the names of stats, and category membership is coded by edges from categories to non-category nodes. If it is a matrix, its columns correspond to values in stats and its rows to the different categories. If matrix element [i,j]!=0, then stats[j] belongs to the i-th categorization.

For situations in which stats is large, the sparse graph representation may be more efficient.

Value

The return value is a list or vector of length equal to the number of categorizations. Each element corresponds to the values obtained by applying FUN to the subset of values in stats according to the category defined for that row.

Author(s)

R. Gentleman, contributions from W. Huber

See Also

split, apply

Examples

 set.seed(123)
 st = rnorm(20)

 aM = matrix(sample(c(0,1), 60, replace=TRUE), nr=3)
 rownames(aM) = paste("category", 1:3, sep="")
 colnames(aM) = names(st) = paste("g", 1:20, sep="")

 bpg = aM2bpG(aM)

 a1 = applyByCategory(st, aM, median)
 a2 = applyByCategory(st, bpg, median)

 stopifnot( identical(a2, a1) )

[Package Category version 1.4.1 Index]