casting: Conversions between matrix types

Description Usage Arguments Details Value Examples

Description

Convenience functions for converting to different sparse matrix formats.

Usage

1
2
3
4
5
as.csr.matrix(x, binary = FALSE)

as.csc.matrix(x, binary = FALSE)

as.coo.matrix(x, binary = FALSE)

Arguments

x

A matrix which is to be converted to a different format.

binary

Whether the result should be a binary-only matrix (inheriting from class ‘nsparseMatrix' - these don’t have slot 'x'). Supported input types are:

  • Sparse matrices from 'Matrix' package, in any format.

  • Sparse vector from 'Matrix' (class 'dsparseVector').

  • Dense matrix from base R.

  • Dense vector from base R (classes 'numeric' and 'integer').

  • Dense matrix or vector from package 'float' (class 'float32').

  • 'data.frame' and 'data.table'.

Details

The functions internally use as(x, "?sparseMatrix"), so they might work with other object classes if they register a conversion method for 'Matrix' base types.

When passed a vector, the functions 'as.csr.matrix' and 'as.coo.matrix' will assume that it is a row vector, while ‘as.csc.matrix' will assume it’s a column vector.

Value

A sparse matrix, with format:

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
library(Matrix)
library(rsparse)

m.coo = as(matrix(1:3), "TsparseMatrix")
as.csr.matrix(m.coo)
as.csr.matrix(1:3) # <- assumes it's a row vector
as.csc.matrix(1:3) # <- assumes it's a column vector

library(float)
m.f32 = float::fl(matrix(1:10, nrow=5))
as.csr.matrix(m.f32)

library(data.table)
as.coo.matrix(data.table(col1=1:3))

dselivanov/rsparse documentation built on Feb. 2, 2021, 2:53 p.m.