Remove extremely sparse columns
remove_sparse.Rd
remove_sparse()
removes columns from sparse and dense matrices that have a
sparsity value greater than a user-defined threshold.
Usage
remove_sparse(x, threshold)
# S3 method for CsparseMatrix
remove_sparse(x, threshold)
# S3 method for matrix
remove_sparse(x, threshold)
Arguments
- x
A
matrix
orCsparseMatrix
.- threshold
A double between 0 and 1 specifying the sparsity threshold at which to remove columns.
Examples
# Create a sparse matrix with very sparse columns
x <- Matrix::rsparsematrix(10, 5, 0.1)
colnames(x) <- paste0("x", 1:5)
# Print x
x
#> 10 x 5 sparse Matrix of class "dgCMatrix"
#> x1 x2 x3 x4 x5
#> [1,] . . . . .
#> [2,] . . . -0.27 .
#> [3,] . . . . .
#> [4,] . . . . .
#> [5,] . . . . .
#> [6,] . . 2.0 . .
#> [7,] 0.6 . . . .
#> [8,] . . . . .
#> [9,] . -0.064 . . .
#> [10,] . . -1.1 . .
# Same matrix in dense format
xdense <- as.matrix(x)
# Drop duplicate columns
remove_sparse(x, threshold = 0.9)
#> 10 x 1 sparse Matrix of class "dgCMatrix"
#> x3
#> [1,] .
#> [2,] .
#> [3,] .
#> [4,] .
#> [5,] .
#> [6,] 2.0
#> [7,] .
#> [8,] .
#> [9,] .
#> [10,] -1.1
remove_sparse(xdense, threshold = 0.9)
#> x3
#> [1,] 0.0
#> [2,] 0.0
#> [3,] 0.0
#> [4,] 0.0
#> [5,] 0.0
#> [6,] 2.0
#> [7,] 0.0
#> [8,] 0.0
#> [9,] 0.0
#> [10,] -1.1