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