get_rowVar: Calculate Row-wise Variance for Dense or Sparse Matrices

View source: R/general_tools.R

get_rowVarR Documentation

Calculate Row-wise Variance for Dense or Sparse Matrices

Description

Efficiently computes the variance of each row for either a base R dense matrix or a sparse dgCMatrix, via a single Rcpp entry point. Logs progress messages to the R console.

Usage

get_rowVar(M, verbose = FALSE)

Arguments

M

A numeric matrix (base R matrix) or a sparse matrix of class "dgCMatrix".

verbose

Logical. If TRUE, prints progress and informational messages. Default is FALSE.

Details

Dispatches in C++ between dense and sparse implementations to avoid unnecessary overhead or external dependencies. Uses compressed-column traversal for sparse inputs.

Value

A numeric vector of length nrow(M) containing the variance of each row.

Note

Only 32-bit integer indices are supported, due to limitations in R's internal matrix representations. This function will not work with matrices that exceed the 32-bit integer indexing range.

Examples

library(Matrix)
# Dense example
dm <- matrix(rnorm(1000), nrow = 100)
get_rowVar(dm)
# Sparse example
sm <- rsparsematrix(100, 10, density = 0.1)
get_rowVar(sm)

splikit documentation built on May 13, 2026, 9:08 a.m.