Transform a Sparse Matrix to Rank Space (Rank Non-zeros in Each Column)
Source:R/util.R
rankSparse.Rd
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.
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