Skip to contents

This function ranks the non-zero elements of a sparse matrix within each column. The ranks are scaled by dividing them by the number of rows in the matrix, resulting in ranks normalized between 0 and 1. The original zero values are left unchanged.

Usage

rankSparse(sparseMat)

Arguments

sparseMat

A sparse matrix

Value

A sparse matrix of the same dimensions as the input, where the non-zero elements are replaced by their ranks within each column, normalized by the number of rows.

Examples

library(Matrix)
# Create a sample sparse matrix
sparseMat <- rsparsematrix(5, 5, density = 0.5)
# Apply the ranking function
rankedSparseMat <- rankSparse(sparseMat)
print(rankedSparseMat)
#> 5 x 5 sparse Matrix of class "dgCMatrix"
#>                         
#> [1,] 0.4 .   0.2 .   0.6
#> [2,] 0.6 .   0.4 .   .  
#> [3,] 0.2 .   .   0.4 0.2
#> [4,] .   0.2 0.6 .   .  
#> [5,] .   .   .   0.2 0.4