This helper function allows easy coercion to sparse matrix objects
from the Matrix package, dgCMatrix
.
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, ...)
Either a list with dgCMatrix
objects or a dgCMatrix
object.
In the case of the igraph
and network
methods, ...
is passed to
as_adj
and as.matrix.network
respectively.
set.seed(1231)
x <- rgraph_er(10)
# From matrix object
as_dgCMatrix(as.matrix(x))
#> 10 x 10 sparse Matrix of class "dgCMatrix"
#> [[ suppressing 10 column names ‘1’, ‘2’, ‘3’ ... ]]
#>
#> 1 . . . . . . . . . .
#> 2 . . . . . . . . . .
#> 3 . . . . . . . . . .
#> 4 . . . . . . . . . .
#> 5 . . . . . . . . . .
#> 6 . . . . . . . . . .
#> 7 . . . . . . . . . .
#> 8 . . . . . . . . . .
#> 9 . . . . . . . . . .
#> 10 . . . . 1 . . . . .
# From a network object
as_dgCMatrix(network::as.network(as.matrix(x)))
#> 10 x 10 sparse Matrix of class "dgCMatrix"
#> [[ suppressing 10 column names ‘1’, ‘2’, ‘3’ ... ]]
#>
#> 1 . . . . . . . . . .
#> 2 . . . . . . . . . .
#> 3 . . . . . . . . . .
#> 4 . . . . . . . . . .
#> 5 . . . . . . . . . .
#> 6 . . . . . . . . . .
#> 7 . . . . . . . . . .
#> 8 . . . . . . . . . .
#> 9 . . . . . . . . . .
#> 10 . . . . 1 . . . . .
# From igraph object
as_dgCMatrix(igraph::graph_from_adjacency_matrix(x))
#> 10 x 10 sparse Matrix of class "dgCMatrix"
#> [[ suppressing 10 column names ‘1’, ‘2’, ‘3’ ... ]]
#>
#> 1 . . . . . . . . . .
#> 2 . . . . . . . . . .
#> 3 . . . . . . . . . .
#> 4 . . . . . . . . . .
#> 5 . . . . . . . . . .
#> 6 . . . . . . . . . .
#> 7 . . . . . . . . . .
#> 8 . . . . . . . . . .
#> 9 . . . . . . . . . .
#> 10 . . . . 1 . . . . .
# From array
myarray <- array(dim=c(10,10,2))
myarray[,,1] <- as.matrix(x)
myarray[,,2] <- as.matrix(x)
myarray
#> , , 1
#>
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [1,] 0 0 0 0 0 0 0 0 0 0
#> [2,] 0 0 0 0 0 0 0 0 0 0
#> [3,] 0 0 0 0 0 0 0 0 0 0
#> [4,] 0 0 0 0 0 0 0 0 0 0
#> [5,] 0 0 0 0 0 0 0 0 0 0
#> [6,] 0 0 0 0 0 0 0 0 0 0
#> [7,] 0 0 0 0 0 0 0 0 0 0
#> [8,] 0 0 0 0 0 0 0 0 0 0
#> [9,] 0 0 0 0 0 0 0 0 0 0
#> [10,] 0 0 0 0 1 0 0 0 0 0
#>
#> , , 2
#>
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [1,] 0 0 0 0 0 0 0 0 0 0
#> [2,] 0 0 0 0 0 0 0 0 0 0
#> [3,] 0 0 0 0 0 0 0 0 0 0
#> [4,] 0 0 0 0 0 0 0 0 0 0
#> [5,] 0 0 0 0 0 0 0 0 0 0
#> [6,] 0 0 0 0 0 0 0 0 0 0
#> [7,] 0 0 0 0 0 0 0 0 0 0
#> [8,] 0 0 0 0 0 0 0 0 0 0
#> [9,] 0 0 0 0 0 0 0 0 0 0
#> [10,] 0 0 0 0 1 0 0 0 0 0
#>
as_dgCMatrix(myarray)
#> $`1`
#> 10 x 10 sparse Matrix of class "dgCMatrix"
#> [[ suppressing 10 column names ‘1’, ‘2’, ‘3’ ... ]]
#>
#> 1 . . . . . . . . . .
#> 2 . . . . . . . . . .
#> 3 . . . . . . . . . .
#> 4 . . . . . . . . . .
#> 5 . . . . . . . . . .
#> 6 . . . . . . . . . .
#> 7 . . . . . . . . . .
#> 8 . . . . . . . . . .
#> 9 . . . . . . . . . .
#> 10 . . . . 1 . . . . .
#>
#> $`2`
#> 10 x 10 sparse Matrix of class "dgCMatrix"
#> [[ suppressing 10 column names ‘1’, ‘2’, ‘3’ ... ]]
#>
#> 1 . . . . . . . . . .
#> 2 . . . . . . . . . .
#> 3 . . . . . . . . . .
#> 4 . . . . . . . . . .
#> 5 . . . . . . . . . .
#> 6 . . . . . . . . . .
#> 7 . . . . . . . . . .
#> 8 . . . . . . . . . .
#> 9 . . . . . . . . . .
#> 10 . . . . 1 . . . . .
#>
# From a diffnet object
ans <- as_dgCMatrix(medInnovationsDiffNet)
str(ans)
#> List of 18
#> $ 1 :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
#> .. ..@ i : int [1:294] 47 8 13 15 28 32 34 32 37 9 ...
#> .. ..@ p : int [1:126] 0 1 1 7 9 12 12 14 15 15 ...
#> .. ..@ Dim : int [1:2] 125 125
#> .. ..@ Dimnames:List of 2
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. ..@ x : num [1:294] 2 1 1 1 1 1 3 1 2 1 ...
#> .. ..@ factors : list()
#> $ 2 :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
#> .. ..@ i : int [1:294] 47 8 13 15 28 32 34 32 37 9 ...
#> .. ..@ p : int [1:126] 0 1 1 7 9 12 12 14 15 15 ...
#> .. ..@ Dim : int [1:2] 125 125
#> .. ..@ Dimnames:List of 2
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. ..@ x : num [1:294] 2 1 1 1 1 1 3 1 2 1 ...
#> .. ..@ factors : list()
#> $ 3 :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
#> .. ..@ i : int [1:294] 47 8 13 15 28 32 34 32 37 9 ...
#> .. ..@ p : int [1:126] 0 1 1 7 9 12 12 14 15 15 ...
#> .. ..@ Dim : int [1:2] 125 125
#> .. ..@ Dimnames:List of 2
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. ..@ x : num [1:294] 2 1 1 1 1 1 3 1 2 1 ...
#> .. ..@ factors : list()
#> $ 4 :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
#> .. ..@ i : int [1:294] 47 8 13 15 28 32 34 32 37 9 ...
#> .. ..@ p : int [1:126] 0 1 1 7 9 12 12 14 15 15 ...
#> .. ..@ Dim : int [1:2] 125 125
#> .. ..@ Dimnames:List of 2
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. ..@ x : num [1:294] 2 1 1 1 1 1 3 1 2 1 ...
#> .. ..@ factors : list()
#> $ 5 :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
#> .. ..@ i : int [1:294] 47 8 13 15 28 32 34 32 37 9 ...
#> .. ..@ p : int [1:126] 0 1 1 7 9 12 12 14 15 15 ...
#> .. ..@ Dim : int [1:2] 125 125
#> .. ..@ Dimnames:List of 2
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. ..@ x : num [1:294] 2 1 1 1 1 1 3 1 2 1 ...
#> .. ..@ factors : list()
#> $ 6 :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
#> .. ..@ i : int [1:294] 47 8 13 15 28 32 34 32 37 9 ...
#> .. ..@ p : int [1:126] 0 1 1 7 9 12 12 14 15 15 ...
#> .. ..@ Dim : int [1:2] 125 125
#> .. ..@ Dimnames:List of 2
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. ..@ x : num [1:294] 2 1 1 1 1 1 3 1 2 1 ...
#> .. ..@ factors : list()
#> $ 7 :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
#> .. ..@ i : int [1:294] 47 8 13 15 28 32 34 32 37 9 ...
#> .. ..@ p : int [1:126] 0 1 1 7 9 12 12 14 15 15 ...
#> .. ..@ Dim : int [1:2] 125 125
#> .. ..@ Dimnames:List of 2
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. ..@ x : num [1:294] 2 1 1 1 1 1 3 1 2 1 ...
#> .. ..@ factors : list()
#> $ 8 :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
#> .. ..@ i : int [1:294] 47 8 13 15 28 32 34 32 37 9 ...
#> .. ..@ p : int [1:126] 0 1 1 7 9 12 12 14 15 15 ...
#> .. ..@ Dim : int [1:2] 125 125
#> .. ..@ Dimnames:List of 2
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. ..@ x : num [1:294] 2 1 1 1 1 1 3 1 2 1 ...
#> .. ..@ factors : list()
#> $ 9 :Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
#> .. ..@ i : int [1:294] 47 8 13 15 28 32 34 32 37 9 ...
#> .. ..@ p : int [1:126] 0 1 1 7 9 12 12 14 15 15 ...
#> .. ..@ Dim : int [1:2] 125 125
#> .. ..@ Dimnames:List of 2
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. ..@ x : num [1:294] 2 1 1 1 1 1 3 1 2 1 ...
#> .. ..@ factors : list()
#> $ 10:Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
#> .. ..@ i : int [1:294] 47 8 13 15 28 32 34 32 37 9 ...
#> .. ..@ p : int [1:126] 0 1 1 7 9 12 12 14 15 15 ...
#> .. ..@ Dim : int [1:2] 125 125
#> .. ..@ Dimnames:List of 2
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. ..@ x : num [1:294] 2 1 1 1 1 1 3 1 2 1 ...
#> .. ..@ factors : list()
#> $ 11:Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
#> .. ..@ i : int [1:294] 47 8 13 15 28 32 34 32 37 9 ...
#> .. ..@ p : int [1:126] 0 1 1 7 9 12 12 14 15 15 ...
#> .. ..@ Dim : int [1:2] 125 125
#> .. ..@ Dimnames:List of 2
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. ..@ x : num [1:294] 2 1 1 1 1 1 3 1 2 1 ...
#> .. ..@ factors : list()
#> $ 12:Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
#> .. ..@ i : int [1:294] 47 8 13 15 28 32 34 32 37 9 ...
#> .. ..@ p : int [1:126] 0 1 1 7 9 12 12 14 15 15 ...
#> .. ..@ Dim : int [1:2] 125 125
#> .. ..@ Dimnames:List of 2
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. ..@ x : num [1:294] 2 1 1 1 1 1 3 1 2 1 ...
#> .. ..@ factors : list()
#> $ 13:Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
#> .. ..@ i : int [1:294] 47 8 13 15 28 32 34 32 37 9 ...
#> .. ..@ p : int [1:126] 0 1 1 7 9 12 12 14 15 15 ...
#> .. ..@ Dim : int [1:2] 125 125
#> .. ..@ Dimnames:List of 2
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. ..@ x : num [1:294] 2 1 1 1 1 1 3 1 2 1 ...
#> .. ..@ factors : list()
#> $ 14:Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
#> .. ..@ i : int [1:294] 47 8 13 15 28 32 34 32 37 9 ...
#> .. ..@ p : int [1:126] 0 1 1 7 9 12 12 14 15 15 ...
#> .. ..@ Dim : int [1:2] 125 125
#> .. ..@ Dimnames:List of 2
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. ..@ x : num [1:294] 2 1 1 1 1 1 3 1 2 1 ...
#> .. ..@ factors : list()
#> $ 15:Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
#> .. ..@ i : int [1:294] 47 8 13 15 28 32 34 32 37 9 ...
#> .. ..@ p : int [1:126] 0 1 1 7 9 12 12 14 15 15 ...
#> .. ..@ Dim : int [1:2] 125 125
#> .. ..@ Dimnames:List of 2
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. ..@ x : num [1:294] 2 1 1 1 1 1 3 1 2 1 ...
#> .. ..@ factors : list()
#> $ 16:Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
#> .. ..@ i : int [1:294] 47 8 13 15 28 32 34 32 37 9 ...
#> .. ..@ p : int [1:126] 0 1 1 7 9 12 12 14 15 15 ...
#> .. ..@ Dim : int [1:2] 125 125
#> .. ..@ Dimnames:List of 2
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. ..@ x : num [1:294] 2 1 1 1 1 1 3 1 2 1 ...
#> .. ..@ factors : list()
#> $ 17:Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
#> .. ..@ i : int [1:294] 47 8 13 15 28 32 34 32 37 9 ...
#> .. ..@ p : int [1:126] 0 1 1 7 9 12 12 14 15 15 ...
#> .. ..@ Dim : int [1:2] 125 125
#> .. ..@ Dimnames:List of 2
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. ..@ x : num [1:294] 2 1 1 1 1 1 3 1 2 1 ...
#> .. ..@ factors : list()
#> $ 18:Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
#> .. ..@ i : int [1:294] 47 8 13 15 28 32 34 32 37 9 ...
#> .. ..@ p : int [1:126] 0 1 1 7 9 12 12 14 15 15 ...
#> .. ..@ Dim : int [1:2] 125 125
#> .. ..@ Dimnames:List of 2
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. .. ..$ : chr [1:125] "1001" "1002" "1003" "1004" ...
#> .. ..@ x : num [1:294] 2 1 1 1 1 1 3 1 2 1 ...
#> .. ..@ factors : list()