Nothing
#' Calculate Matrix Column Variance
#'
#' `col_vars` computes the sample variance for each column of a numeric matrix.
#'
#' @param mat A numeric matrix.
#' @param cores Number of cores to use for parallel computation. Defaults to 1.
#'
#' @details
#' Variances for columns with one unique value after dropping `NA` are set to `NA`.
#'
#' @returns `col_vars` returns a named numeric vector of column variances.
#'
#' @export
#'
#' @examples
#' mat <- matrix(rnorm(4 * 10), ncol = 4)
#' mat[1, 1] <- NA
#' mat[1:8, 2] <- NA
#' mat[1:9, 3] <- NA
#' mat[, 4] <- NA
#' mat
#' col_vars(mat)
#' apply(mat, 2, var, na.rm = TRUE)
col_vars <- function(mat, cores = 1) {
checkmate::assert_matrix(
mat,
mode = "numeric", null.ok = FALSE, min.rows = 1, min.cols = 1, .var.name = "mat"
)
checkmate::assert_int(cores, lower = 1)
vars <- col_vars_internal(mat = mat, cores = cores)[1, ]
vars[is.nan(vars)] <- NA
names(vars) <- colnames(mat)
return(vars)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.