# rowAvgsPerColSet.matrix: Applies a row-by-row (column-by-column) averaging function to... In matrixStats: Methods that apply to rows and columns of a matrix

## Description

Applies a row-by-row (column-by-column) averaging function to equally-sized subsets of matrix columns (rows). Each subset is averaged independently of the others.

## Usage

 ```1 2``` ```## S3 method for class 'matrix' rowAvgsPerColSet(X, W=NULL, S, FUN=rowMeans, ..., tFUN=FALSE) ```

## Arguments

 `X` A `numeric` NxM `matrix`. `W` An optional `numeric` NxM `matrix` of weights. `S` An `integer` KxJ `matrix` specifying the J subsets. Each column holds K column (row) indices for the corresponding subset. `FUN` The row-by-row (column-by-column) `function` used to average over each subset of `X`. This function must accept a `numeric` NxK (KxM) `matrix` and the `logical` argument `na.rm` (which is automatically set), and return a `numeric` `vector` of length N (M). `...` Additional arguments passed to then `FUN` `function`. `tFUN` If `TRUE`, the NxK (KxM) `matrix` passed to `FUN()` is transposed first.

## Details

If argument `S` is a single column vector with indices `1:N`, then `rowAvgsPerColSet(X, S=S, FUN=rowMeans)` gives the same result as `rowMeans(X)`. Analogously, for `rowAvgsPerColSet()`.

## Value

Returns a `numeric` JxN (MxJ) `matrix`, where row names equal `rownames(X)` (`colnames(S)`) and column names `colnames(S)` (`colnames(X)`).

Henrik Bengtsson

## 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 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 56 57 58 59 60 61 62 63 64 65 66``` ```X <- matrix(rnorm(20*6), nrow=20, ncol=6) rownames(X) <- LETTERS[1:nrow(X)] colnames(X) <- letters[1:ncol(X)] print(X) # - - - - - - - - - - - - - - - - - - - - - - - - - - # Apply rowMeans() for 3 sets of 2 columns # - - - - - - - - - - - - - - - - - - - - - - - - - - nbrOfSets <- 3 S <- matrix(1:ncol(X), ncol=nbrOfSets) colnames(S) <- sprintf("s%d", 1:nbrOfSets) print(S) Z <- rowAvgsPerColSet(X, S=S) print(Z) # Validation Z0 <- cbind(s1=rowMeans(X[,1:2]), s2=rowMeans(X[,3:4]), s3=rowMeans(X[,5:6])) stopifnot(identical(drop(Z), Z0)) # - - - - - - - - - - - - - - - - - - - - - - - - - - # Apply colMeans() for 5 sets of 4 rows # - - - - - - - - - - - - - - - - - - - - - - - - - - nbrOfSets <- 5 S <- matrix(1:nrow(X), ncol=nbrOfSets) colnames(S) <- sprintf("s%d", 1:nbrOfSets) print(S) Z <- colAvgsPerRowSet(X, S=S) print(Z) # Validation Z0 <- rbind(s1=colMeans(X[1:4,]), s2=colMeans(X[5:8,]), s3=colMeans(X[9:12,]), s4=colMeans(X[13:16,]), s5=colMeans(X[17:20,])) stopifnot(identical(drop(Z), Z0)) # - - - - - - - - - - - - - - - - - - - - - - - - - - # When there is only one "complete" set # - - - - - - - - - - - - - - - - - - - - - - - - - - nbrOfSets <- 1 S <- matrix(1:ncol(X), ncol=nbrOfSets) colnames(S) <- sprintf("s%d", 1:nbrOfSets) print(S) Z <- rowAvgsPerColSet(X, S=S, FUN=rowMeans) print(Z) Z0 <- rowMeans(X) stopifnot(identical(drop(Z), Z0)) nbrOfSets <- 1 S <- matrix(1:nrow(X), ncol=nbrOfSets) colnames(S) <- sprintf("s%d", 1:nbrOfSets) print(S) Z <- colAvgsPerRowSet(X, S=S, FUN=colMeans) print(Z) Z0 <- colMeans(X) stopifnot(identical(drop(Z), Z0)) ```

matrixStats documentation built on May 31, 2017, 2:26 a.m.