R/zscores.from.metrics.R

Defines functions zscores.from.metrics

Documented in zscores.from.metrics

#' Calculate z-scores for each metric across each sample
#'
#' This function takes a dataframe of QC metrics, and calculates the
#' the z-scores. If filename is specified, the results will be saved to file.
#'
#' @param qc.data A dataframe whose rows are samples and each column a QC metric
#' @param filename A filename where to save data. If NULL data will not be saved to file
#' @return A dataframe of z-scores; rows correspond to samples and columns correspond to metrics from `qc.data`
#' @export
zscores.from.metrics <- function(qc.data, filename = NULL) {

    zscore.format.check(qc.data);

    zscores <- apply(qc.data, 2, function(x) scale(x, center = TRUE, scale = TRUE));

    rownames(zscores) <- row.names(qc.data);

    if (!is.null(filename)) {
        utils::write.table(
            x = zscores,
            file = filename,
            quote = FALSE,
            row.names = TRUE,
            col.names = TRUE
            );
        }

    return(zscores);
    }

Try the OmicsQC package in your browser

Any scripts or data that you put into this service are public.

OmicsQC documentation built on May 29, 2024, 11:14 a.m.