slice: Sparse Matrices Slicing

sliceR Documentation

Sparse Matrices Slicing

Description

Natively slice CSR/CSC/COO matrices without changing the storage order.

Usage

## S4 method for signature 'RsparseMatrix,index,index,logical'
x[i, j, drop]

## S4 method for signature 'RsparseMatrix,missing,index,logical'
x[i, j, drop]

## S4 method for signature 'RsparseMatrix,index,missing,logical'
x[i, j, drop]

## S4 method for signature 'RsparseMatrix,missing,missing,logical'
x[i, j, drop]

## S4 method for signature 'RsparseMatrix,index,index,missing'
x[i, j, drop]

## S4 method for signature 'RsparseMatrix,missing,index,missing'
x[i, j, drop]

## S4 method for signature 'RsparseMatrix,index,missing,missing'
x[i, j, drop]

## S4 method for signature 'RsparseMatrix,missing,missing,missing'
x[i, j, drop]

## S4 method for signature 'ANY,nsparseVector,nsparseVector,logical'
x[i, j, drop]

## S4 method for signature 'ANY,missing,nsparseVector,logical'
x[i, j, drop]

## S4 method for signature 'ANY,nsparseVector,missing,logical'
x[i, j, drop]

## S4 method for signature 'ANY,index,nsparseVector,logical'
x[i, j, drop]

## S4 method for signature 'ANY,nsparseVector,index,logical'
x[i, j, drop]

## S4 method for signature 'ANY,nsparseVector,nsparseVector,missing'
x[i, j, drop]

## S4 method for signature 'ANY,missing,nsparseVector,missing'
x[i, j, drop]

## S4 method for signature 'ANY,nsparseVector,missing,missing'
x[i, j, drop]

## S4 method for signature 'ANY,index,nsparseVector,missing'
x[i, j, drop]

## S4 method for signature 'ANY,nsparseVector,index,missing'
x[i, j, drop]

## S4 method for signature 'ANY,lsparseVector,lsparseVector,logical'
x[i, j, drop]

## S4 method for signature 'ANY,missing,lsparseVector,logical'
x[i, j, drop]

## S4 method for signature 'ANY,lsparseVector,missing,logical'
x[i, j, drop]

## S4 method for signature 'ANY,index,lsparseVector,logical'
x[i, j, drop]

## S4 method for signature 'ANY,lsparseVector,index,logical'
x[i, j, drop]

## S4 method for signature 'ANY,lsparseVector,lsparseVector,missing'
x[i, j, drop]

## S4 method for signature 'ANY,missing,lsparseVector,missing'
x[i, j, drop]

## S4 method for signature 'ANY,lsparseVector,missing,missing'
x[i, j, drop]

## S4 method for signature 'ANY,index,lsparseVector,missing'
x[i, j, drop]

## S4 method for signature 'ANY,lsparseVector,index,missing'
x[i, j, drop]

## S4 method for signature 'CsparseMatrix,index,index,logical'
x[i, j, drop]

## S4 method for signature 'CsparseMatrix,missing,index,logical'
x[i, j, drop]

## S4 method for signature 'CsparseMatrix,index,missing,logical'
x[i, j, drop]

## S4 method for signature 'CsparseMatrix,missing,missing,logical'
x[i, j, drop]

## S4 method for signature 'CsparseMatrix,index,index,missing'
x[i, j, drop]

## S4 method for signature 'CsparseMatrix,missing,index,missing'
x[i, j, drop]

## S4 method for signature 'CsparseMatrix,index,missing,missing'
x[i, j, drop]

## S4 method for signature 'CsparseMatrix,missing,missing,missing'
x[i, j, drop]

## S4 method for signature 'TsparseMatrix,index,index,logical'
x[i, j, drop]

## S4 method for signature 'TsparseMatrix,missing,index,logical'
x[i, j, drop]

## S4 method for signature 'TsparseMatrix,index,missing,logical'
x[i, j, drop]

## S4 method for signature 'TsparseMatrix,missing,missing,logical'
x[i, j, drop]

## S4 method for signature 'TsparseMatrix,index,index,missing'
x[i, j, drop]

## S4 method for signature 'TsparseMatrix,missing,index,missing'
x[i, j, drop]

## S4 method for signature 'TsparseMatrix,index,missing,missing'
x[i, j, drop]

## S4 method for signature 'TsparseMatrix,missing,missing,missing'
x[i, j, drop]

Arguments

x

A sparse matrix to subset, in any format.

i

row indices to subset.

j

column indices to subset.

drop

whether to simplify 1d matrix to a vector

Details

Important: When slicing sparse matrices with 'drop=TRUE' (the default), 'Matrix' will drop 1-d matrices to dense dense vectors, whereas this package allows dropping them to either dense or sparse vectors, the latter of which is more efficient and is the default option.

The 'drop' behavior can be changed back to dense vectors like 'Matrix' does, through restore_old_matrix_behavior or through the package options (e.g. 'options("MatrixExtra.drop_sparse" = FALSE)' - see MatrixExtra-options).

Note: Trying to slice a sparse matrix without supplying any parameter for the second axis (e.g. 'X[1:10]') will select whole rows (as if it were 'X[1:10,]') instead of selecting entries as if the input were a flattened array (which is what 'Matrix' and base R do).

This package will override the subsetting methods from 'Matrix' for all sparse matrix types. It is usually much faster for all three storage orders (especially CSR) but in some situations could end up being slightly slower. Be aware that, in the case of COO matrices (a.k.a. "TsparseMatrix"), the resulting object will not have sorted indices, which 'Matrix' will oftentimes do in addition to subsetting, at a large speed penalty.

In general, it's much faster to select rows when the input is a CSR matrix ("RsparseMatrix"), and much faster to select columns when the input is a CSC matrix ("CsparseMatrix"). Slicing COO matrices is typically not efficient, but could end up being faster when the slice involves random rows and random columns with repeated entries.

Value

A sparse matrix with the same storage order and dtype as 'x'.

Examples

library(Matrix)
library(MatrixExtra)
m <- rsparsematrix(20, 20, 0.1, repr="R")
inherits(m[1:2, ], "RsparseMatrix")
inherits(m[1:2, 3:4], "RsparseMatrix")
inherits(as.coo.matrix(m)[1:2, 3:4], "TsparseMatrix")
inherits(as.csc.matrix(m)[1:2, 3:4], "CsparseMatrix")

### New: slice with a sparse vector
m[as(c(TRUE,FALSE), "sparseVector"), ]

### Important!!!
### This differs from Matrix
set_new_matrix_behavior()
inherits(m[1,,drop=TRUE], "sparseVector")

### To bring back the old behavior:
restore_old_matrix_behavior()
inherits(m[1,,drop=TRUE], "numeric")

MatrixExtra documentation built on May 29, 2024, 2:53 a.m.