View source: R/collapse_matrix.r
| collapse_matrix | R Documentation |
Takes a two-dimensional matrix and collapses row or columns according to a specified function.
collapse_matrix(x, groups, dim = 1, FUN = sum)
x |
A |
groups |
A vector or factor of the same length as the rows or columns of x, which indicates which elements to collapse. |
dim |
Which dimension to collapse. 1 for rows and 2 for columns |
FUN |
Function (or function name) to apply to elements per groups |
This function uses plyr functions to aggregate columnwise or rowwise according to a grouping factor.
Returns a matrix object.
Sur Herrera Paredes
x <- cbind(rbind(matrix(0,2,2),matrix(1,2,2)),
rbind(matrix(1,2,2),matrix(0,2,2)))
colnames(x) <- c("C1","C2","C3","C4")
row.names(x) <- c("R1","R2","R3","R4")
x
collapse_matrix(x=x,groups = rep(c(1,2),each=2),dim=1,FUN=sum)
collapse_matrix(x=x,groups = rep(c(1,2),each=2),dim=2,FUN=mean)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.