R/aggregatedMean.R

Defines functions aggregatedMean

Documented in aggregatedMean

#' Aggregated Mean Scores
#'
#'
#' @param data a \code{\link[data.table]{data.table}} containing the data, where
#' the columns correspond to the variables and the rows to the samples
#' @param group A list containing the grouping corresponding to the variables.
#' If this is \code{NULL} all variables will be treated as they were in the same
#' group and only one representative will be calculated
#'
#' @export
#' @return matrix of aggregated mean values for each group x sample
#' @examples
#' data(qmdiab.data)
#' data(qmdiab.annos)
#' scaled.data <- scale(qmdiab.data)
#' aggregated.z.scores <- aggregatedMean(
#'   data = scaled.data,
#'   group = qmdiab.annos$Sub.pathway
#' )
aggregatedMean <- function(data, group) {
  unique.group <- unique(group)
  res <- sapply(unique.group, function(g) {
    apply(as.data.frame(data[, group == g]),
      1, mean,
      na.rm = TRUE
    )
  })
  colnames(res) <- unique.group
  return(res)
}
krumsiek/MoDentify documentation built on March 25, 2021, 8:32 a.m.