Skip to contents

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.

Value

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

Details

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

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)
#>   [1] 0.8159298 0.3423491 0.6131506 1.1955451 0.8142368 0.6751831 0.7977379
#>   [8] 0.1802766 0.6204153 1.0053747 0.2989410 0.6265333 0.6965555 0.9845248
#>  [15] 0.5192824 0.6998138 0.4638970 0.4076945 1.1371958 1.1910569 0.7896014
#>  [22] 0.6633833 0.6336162 0.6433796 0.8076650 0.8381046 1.1119319 0.8387923
#>  [29] 1.0978441 0.9939346 1.3430917 1.1521715 0.7607957 1.1865619 1.3196188
#>  [36] 0.7133707 0.4791377 0.7249748 0.3659073 0.7915175 1.5232523 0.7068475
#>  [43] 1.5126126 1.1832296 1.1013531 1.3929765 0.8665166 0.8867069 0.5626939
#>  [50] 1.0600832 0.7738047 1.0173623 0.8023192 0.5426145 0.7044572 0.7622790
#>  [57] 0.7212168 1.0327142 1.9541594 1.3982414 0.4204855 1.3057406 0.8610861
#>  [64] 1.0717392 1.2720646 1.2057575 0.5448984 0.3519345 1.1059378 1.0939192
#>  [71] 0.4542121 1.1126801 0.6741042 0.2799314 0.4097694 0.4169646 0.5938357
#>  [78] 0.9796577 0.5444346 0.5856278 1.0696032 0.4247272 0.8322548 0.6371772
#>  [85] 0.4054830 1.7754385 1.0234854 1.5279700 0.5216154 1.9838665 0.7297534
#>  [92] 0.8460690 0.4382975 0.6220660 1.6951250 0.6111148 0.8490081 0.9696695
#>  [99] 2.0163384 0.8447419
# Sparse example
sm <- rsparsematrix(100, 10, density = 0.1)
get_rowVar(sm)
#>   [1] 0.6347344100 0.3147800000 0.0000000000 0.0000000000 0.0000000000
#>   [6] 0.0874090000 0.0000000000 0.3444000000 0.0745290000 0.0000000000
#>  [11] 0.0182250000 0.0840360000 0.1296000000 0.0000000000 0.0928890000
#>  [16] 0.0000000000 0.0219840000 0.0466560000 0.0000000000 0.0864360000
#>  [21] 0.1846440000 0.0732960000 0.1533210000 0.0516710400 0.3225000000
#>  [26] 0.0000000000 0.0000000000 0.0000000000 0.0000000000 0.0158760000
#>  [31] 0.1764000000 0.4761000000 0.0000000000 0.5316000000 0.0000000000
#>  [36] 0.0000000000 0.0829440000 0.0000000000 0.0334890000 0.6561000000
#>  [41] 0.2295840000 0.0882090000 0.3352040000 0.0000000000 0.0000000000
#>  [46] 0.2583840000 0.0010890000 0.0590490000 0.0492840000 0.0380250000
#>  [51] 0.2092360000 0.0000000000 0.2990250000 0.0492840000 0.0252810000
#>  [56] 0.0665640000 0.0000000000 0.0000000000 0.2025000000 0.0000000000
#>  [61] 0.0248010000 0.0000000000 0.1035240000 0.2600250000 0.0000000000
#>  [66] 0.1089000000 0.1089000000 0.2268960000 0.0000000000 0.0000000000
#>  [71] 0.0012960000 0.0000000000 0.0190440000 0.0000000000 0.0380250000
#>  [76] 0.3296160000 0.0838410000 0.0900000000 0.0364010000 0.0000000000
#>  [81] 0.0000000000 0.1484490000 0.0216090000 0.0190440000 0.0000000000
#>  [86] 0.0000000000 0.0000000000 0.3590760000 0.0158760000 0.0000000000
#>  [91] 0.0380250000 0.1743360000 0.2389210000 0.0000000000 0.0795240000
#>  [96] 0.0002115081 0.0302760000 0.0000000000 0.0000000000 0.0000705600