Remove empty (all-zero) columns
remove_empty.Rd
remove_empty()
removes all-zero columns from sparse and dense matrices.
Usage
remove_empty(x)
# S3 method for CsparseMatrix
remove_empty(x)
# S3 method for matrix
remove_empty(x)
Examples
# Create a sparse matrix with constant columns
x <- Matrix::rsparsematrix(10, 3, 0.1)
x <- cbind(x, 0)
colnames(x) <- paste0("x", 1:4)
# Print x
x
#> 10 x 4 sparse Matrix of class "dgCMatrix"
#> x1 x2 x3 x4
#> [1,] . . . .
#> [2,] . . . .
#> [3,] 0.84 . . .
#> [4,] . . . .
#> [5,] . . . .
#> [6,] . . -1.3 .
#> [7,] . . . .
#> [8,] 0.51 . . .
#> [9,] . . . .
#> [10,] . . . .
# Same matrix in dense format
xdense <- as.matrix(x)
# Drop empty columns
remove_empty(x)
#> 10 x 2 sparse Matrix of class "dgCMatrix"
#> x1 x3
#> [1,] . .
#> [2,] . .
#> [3,] 0.84 .
#> [4,] . .
#> [5,] . .
#> [6,] . -1.3
#> [7,] . .
#> [8,] 0.51 .
#> [9,] . .
#> [10,] . .
remove_empty(xdense)
#> x1 x3
#> [1,] 0.00 0.0
#> [2,] 0.00 0.0
#> [3,] 0.84 0.0
#> [4,] 0.00 0.0
#> [5,] 0.00 0.0
#> [6,] 0.00 -1.3
#> [7,] 0.00 0.0
#> [8,] 0.51 0.0
#> [9,] 0.00 0.0
#> [10,] 0.00 0.0