col_vars: Calculate Matrix Column Variances

View source: R/col_vars.R

col_varsR Documentation

Calculate Matrix Column Variances

Description

Compute the sample variance for each column of a numeric matrix.

Usage

col_vars(obj, cores = 1)

Arguments

obj

A numeric matrix.

cores

Integer. Number of cores to use for parallel computation. Defaults to 1.

Details

Columns with fewer than two non-missing values are assigned NA.

NA/NaN are treated as missing and dropped (equivalent to var(x, na.rm = TRUE)). Inf/-Inf are not missing. They enter the arithmetic, so a column's variance can be NaN, matching base R.

Value

A numeric vector of column variances, named if obj has column names.

Examples

set.seed(123)
obj <- matrix(rnorm(7 * 10), ncol = 7)
obj[1, 1] <- Inf
obj[1, 2] <- NA
obj[1:8, 3] <- NA
obj[8, 3] <- Inf
obj[1:8, 4] <- NA
obj[1:8, 5] <- NA
obj[9, 5] <- obj[10, 5]
obj[1:9, 6] <- NA
obj[, 7] <- NA
obj

col_vars(obj)
apply(obj, 2, var, na.rm = TRUE)

slideimp documentation built on June 17, 2026, 1:08 a.m.