R Under development (unstable) (2026-01-21 r89314 ucrt) -- "Unsuffered Consequences" Copyright (C) 2026 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library(ribiosUtils) > my.matrix <- matrix(1:36, nrow=6) > print(my.matrix) [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 7 13 19 25 31 [2,] 2 8 14 20 26 32 [3,] 3 9 15 21 27 33 [4,] 4 10 16 22 28 34 [5,] 5 11 17 23 29 35 [6,] 6 12 18 24 30 36 > > my.factor <- factor(c("A", "B", "A", "C", "B", "B")) > rowm <- summarizeRows(matrix=my.matrix, factor=my.factor, fun=mean) > rowm.exp <- matrix(c(2,8,14,20,26,32,13/3, 31/3, 49/3, 67/3, 85/3, 103/3, 4,10,16,22,28,34), nrow=3, byrow=TRUE, dimnames=list(c("A","B","C"),NULL)) > stopifnot(identical(rowm,rowm.exp)) > > rowp <- summarizeRows(matrix=my.matrix, factor=my.factor, fun=prod) > rowp.exp <- matrix(c(3, 63, 195,399, 675,1023, 60, 1056, 4284, 11040, 22620, 40320, 4, 10,16,22,28,34), nrow=3, byrow=TRUE, dimnames=list(c("A","B","C"),NULL)) > stopifnot(identical(rowp, rowp.exp)) > > colm <- summarizeColumns(matrix=my.matrix, factor=my.factor, fun=mean) > colm.exp <- matrix(as.numeric(c(7:12, 21:26, 19:24)), ncol=3, byrow=FALSE,dimnames=list(NULL, c("A", "B","C"))) > stopifnot(identical(colm, colm.exp)) > > colp <- summarizeColumns(matrix=my.matrix, factor=my.factor, fun=prod) > colp.exp <- matrix(c(13,28,45,64,85,108, 5425, 6656, 8019, 9520, 11165,12960, 19,20,21,22,23,24), ncol=3, byrow=FALSE, dimnames=list(NULL, c("A", "B","C"))) > stopifnot(identical(colp, colp.exp)) > > ## NA values in factor > my.na.factor <- factor(c("A", "B", "A", "C", NA, "B")) > rowmn <- summarizeRows(matrix=my.matrix, factor=my.na.factor, fun=mean) > rowmn.exp <- matrix(c(2,8,14,20,26,32,4,10,16,22,28,34, 4,10,16,22,28,34), nrow=3, byrow=TRUE, dimnames=list(c("A","B","C"),NULL)) > stopifnot(identical(rowmn, rowmn.exp)) > > rowpn <- summarizeRows(matrix=my.matrix, factor=my.na.factor, fun=prod) > rowpn.exp <- matrix(c(3, 63, 195,399, 675,1023, 12, 96, 252, 480, 780, 1152, 4, 10,16,22,28,34), nrow=3, byrow=TRUE, dimnames=list(c("A","B","C"),NULL)) > stopifnot(identical(rowpn, rowpn.exp)) > > colmn <- summarizeColumns(matrix=my.matrix, factor=my.na.factor, fun=mean) > colmn.exp <- matrix(as.numeric(c(7:12, 19:24, 19:24)), ncol=3, byrow=FALSE,dimnames=list(NULL, c("A", "B","C"))) > stopifnot(identical(colmn, colmn.exp)) > > colpn <- summarizeColumns(matrix=my.matrix, factor=my.na.factor, fun=prod) > colpn.exp <- matrix(c(13,28,45,64,85,108, 217, 256, 297, 340, 385,432, 19,20,21,22,23,24), ncol=3, byrow=FALSE, dimnames=list(NULL, c("A", "B","C"))) > stopifnot(identical(colpn, colpn.exp)) > > proc.time() user system elapsed 0.12 0.04 0.15