as_dgCMatrix: Coerce a matrix-like objects to 'dgCMatrix' (sparse matrix)

View source: R/misc.r

as_dgCMatrixR Documentation

Coerce a matrix-like objects to dgCMatrix (sparse matrix)

Description

This helper function allows easy coercion to sparse matrix objects from the Matrix package, dgCMatrix.

Usage

as_dgCMatrix(x, make.dimnames = TRUE, ...)

as.dgCMatrix(x, make.dimnames = TRUE, ...)

as_spmat(x, make.dimnames = TRUE, ...)

## Default S3 method:
as_dgCMatrix(x, make.dimnames = TRUE, ...)

## S3 method for class 'diffnet'
as_dgCMatrix(x, make.dimnames = TRUE, ...)

## S3 method for class 'array'
as_dgCMatrix(x, make.dimnames = TRUE, ...)

## S3 method for class 'igraph'
as_dgCMatrix(x, make.dimnames = TRUE, ...)

## S3 method for class 'network'
as_dgCMatrix(x, make.dimnames = TRUE, ...)

## S3 method for class 'list'
as_dgCMatrix(x, make.dimnames = TRUE, ...)

Arguments

x

An object to be coerced into a sparse matrix.

make.dimnames

Logical scalar. When TRUE, it makes sure that the returned object has dimnames.

...

Further arguments passed to the method.

Details

In the case of the igraph and network methods, ... is passed to as_adj and as.matrix.network respectively.

Value

Either a list with dgCMatrix objects or a dgCMatrix object.

Examples


set.seed(1231)
x <- rgraph_er(10)

# From matrix object
as_dgCMatrix(as.matrix(x))

# From a network object
as_dgCMatrix(network::as.network(as.matrix(x)))

# From igraph object
as_dgCMatrix(igraph::graph_from_adjacency_matrix(x))

# From array
myarray <- array(dim=c(10,10,2))
myarray[,,1] <- as.matrix(x)
myarray[,,2] <- as.matrix(x)

myarray
as_dgCMatrix(myarray)

# From a diffnet object
ans <- as_dgCMatrix(medInnovationsDiffNet)
str(ans)



netdiffuseR documentation built on Aug. 30, 2023, 5:07 p.m.