R/averageMatrix.r

Defines functions averageMatrix

Documented in averageMatrix

#' Average a transformed-counts matrix before ploting a heatmap
#'
#' Average a transformed-counts matrix before ploting a heatmap
#'
#' @param counts.trans \code{matrix} of transformed counts (VST or rlog for DESeq2 or CPM for edgeR)
#' @param target target \code{data.frame} of the project
#' @param varInt variable(s) on which sorting the target
#' @return A matrix of varInt-averaged transformed-counts
#' @author Hugo Varet

# created Nov 23rd, 2015

averageMatrix <- function(counts.trans, target, varInt=NULL){
  if (!is.null(varInt)){
    counts.trans.mean <- t(aggregate(t(counts.trans), by=list(group=apply(as.data.frame(target[,varInt]),1,paste,collapse=".")
), FUN=mean))
    colnames(counts.trans.mean) <- counts.trans.mean[1,]
    counts.trans.mean <- counts.trans.mean[-1,]
    counts.trans.mean <- apply(counts.trans.mean, 2, as.numeric)
    rownames(counts.trans.mean) <- rownames(counts.trans)
    out <- counts.trans.mean
  } else{
    out <- counts.trans
  }
  out
}
biomics-pasteur-fr/RNADiff documentation built on Aug. 27, 2020, 12:44 a.m.