R/apply_transformations.R

#' Apply a list of transformations across a matrix.
#'
#' @param X A matrix.
#' @param transformations A list of functions to apply to corresponding columns.
#' @param indices The columns of the matrix for which to apply the transformation.
#' @return A matrix (or vector) of transformed values.
applyTransformations <- function(X, transformations, indices = NULL) {
    x_mat <- as.matrix(X)
    p <- ncol(x_mat)
    if (p != length(transformations)) {
        stop("The number of transformations must be equal to the number of variables.")
    }
    if (is.null(indices)) {
        indices <- 1:p
    }
    y_mat <- do.call(cbind, lapply(indices, function(index) {
        transformations[[index]](x_mat[, index, drop = TRUE])
    }))
    y_mat
}
burrisk/midamix documentation built on June 1, 2019, 12:49 p.m.