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] 1.1018485 0.8131798 0.8674851 0.5473593 0.4231393 0.4349281 2.0527217
#>   [8] 2.1355356 0.2889168 0.5953774 0.7831572 0.6313660 0.5697153 1.6387679
#>  [15] 0.2646404 0.5815421 0.9296110 0.7288198 0.7878216 0.3887527 0.7083624
#>  [22] 1.2600654 0.8642284 0.5749854 0.4003435 1.3505862 0.3440201 0.3730854
#>  [29] 0.1846048 1.1179794 0.6194398 1.2214748 2.1889284 0.6848380 0.4185397
#>  [36] 0.9369517 0.7385785 0.9752599 0.9759561 0.6877087 0.8918156 0.4846467
#>  [43] 0.4064346 0.8913019 0.7182090 1.7084086 0.8953653 0.4782777 0.2876573
#>  [50] 1.4908286 1.2127783 1.1764224 0.5782282 1.0814132 1.0098213 1.0562451
#>  [57] 1.2325942 0.9705036 0.4763512 1.5715200 1.0308201 0.8333828 0.4267067
#>  [64] 0.6059057 0.3100753 0.8874616 0.6265117 1.7380017 1.4644625 0.4988354
#>  [71] 0.8330486 1.3693059 0.6726239 0.7648966 0.6661642 0.9042962 0.7454102
#>  [78] 0.6124320 1.6726500 1.1819918 0.6584960 0.4566221 0.4357593 0.9967380
#>  [85] 1.3613899 1.5629762 0.6502934 0.4795608 0.9047009 1.0711466 1.3340828
#>  [92] 0.8695552 0.2554259 1.0298180 1.0553800 0.8668971 0.6449623 0.4129988
#>  [99] 1.3039012 0.8084426
# Sparse example
sm <- rsparsematrix(100, 10, density = 0.1)
get_rowVar(sm)
#>   [1] 0.00000000 0.04410000 0.00000000 0.31624100 0.03686400 0.06502500
#>   [7] 0.36000000 0.07128900 0.00000000 0.00000000 0.00000000 0.17640000
#>  [13] 0.09000000 0.00000000 0.23040000 0.06656400 0.00000000 0.00000000
#>  [19] 0.36810000 0.00000000 0.00000000 0.42760000 0.04107600 0.00000000
#>  [25] 0.05216400 0.00000000 0.05840100 0.47610000 0.06502500 0.00000000
#>  [31] 0.35904400 0.03459600 0.01102500 0.00000000 0.36000000 0.29160000
#>  [37] 0.00000000 0.03920400 0.12960000 0.00656100 0.00000000 0.00000000
#>  [43] 0.00000000 0.32490000 0.01988100 0.03348900 0.00000000 0.00000000
#>  [49] 0.00000000 0.51338000 0.00019044 0.02433600 0.02340900 0.26010000
#>  [55] 0.00000000 0.10890000 0.00000000 0.00000000 0.00000000 0.08944900
#>  [61] 0.00000000 0.00000000 0.13788900 0.32490000 0.00000000 0.25013849
#>  [67] 0.00000000 0.02073600 0.05336100 0.00921600 0.12892416 0.05794500
#>  [73] 0.15214972 0.09000000 0.00000000 0.00000000 0.00000000 0.15128064
#>  [79] 0.08930641 0.21254400 0.01368900 0.02073600 0.00000000 0.00000000
#>  [85] 0.05432900 0.00000000 0.37610000 0.27840000 0.25987600 0.00000000
#>  [91] 0.23894500 0.00000000 0.10890000 0.29160000 0.15210000 0.74392000
#>  [97] 0.00705600 0.25697600 0.11156100 0.15210000