How to convert a sparse matrix into a matrix in R?


A sparse matrix is a type of matrix that has most of the elements equal to zero but there is no restriction for the number of zero elements. As a general criterion the number of non-zero elements are expected to be equal to the number of rows or number of columns. To convert a sparse matrix into a matrix R, we can use as.matrix function with the sparse matrix object name.

Example1

 Live Demo

library(Matrix)
i<−c(1,5,2,4,2,2,8);j<−c(2,5,3,2,4,2,4);x<−rpois(7,2)
M1<−sparseMatrix(i,j,x=x)
M1
8 x 5 sparse Matrix of class "dgCMatrix"

Output

[1,] . 3 . . .
[2,] . 5 3 3 .
[3,] . . . . .
[4,] . 2 . . .
[5,] . . . . 4
[6,] . . . . .
[7,] . . . . .
[8,] . . . 3 .

Example

M1<−as.matrix(M1)
M1

Output

[,1] [,2] [,3] [,4] [,5]
[1,] 0 3 0 0 0
[2,] 0 5 3 3 0
[3,] 0 0 0 0 0
[4,] 0 2 0 0 0
[5,] 0 0 0 0 4
[6,] 0 0 0 0 0
[7,] 0 0 0 0 0
[8,] 0 0 0 3 0

Example2

 Live Demo

i<−c(1,3,8,4,2,7,6,9,1,4,10);j<−c(2,5,3,2,4,2,4,5,2,7,3);x<−rpois(11,2)
M2<−sparseMatrix(i,j,x=x)
M2
10 x 7 sparse Matrix of class "dgCMatrix"

Output

[1,] . 3 . . . . .
[2,] . . . 0 . . .
[3,] . . . . 3 . .
[4,] . 1 . . . . 3
[5,] . . . . . . .
[6,] . . . 0 . . .
[7,] . 4 . . . . .
[8,] . . 2 . . . .
[9,] . . . . 1 . .
[10,] . . 1 . . . .

Example

M2<−as.matrix(M2)
M2

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] 0 3 0 0 0 0 0
[2,] 0 0 0 0 0 0 0
[3,] 0 0 0 0 3 0 0
[4,] 0 1 0 0 0 0 3
[5,] 0 0 0 0 0 0 0
[6,] 0 0 0 0 0 0 0
[7,] 0 4 0 0 0 0 0
[8,] 0 0 2 0 0 0 0
[9,] 0 0 0 0 1 0 0
[10,] 0 0 1 0 0 0 0

Example3

 Live Demo

i<−c(1,3,8,4,2,7,6,9,1,4,10);j<−c(2,5,3,2,4,2,4,5,2,7,3);x<−rpois(11,5)
M3<−sparseMatrix(i,j,x=x)
M3
10 x 7 sparse Matrix of class "dgCMatrix"

Output

[1,] . 8 . . . . .
[2,] . . . 8 . . .
[3,] . . . . 3 . .
[4,] . 7 . . . . 7
[5,] . . . . . . .
[6,] . . . 8 . . .
[7,] . 3 . . . . .
[8,] . . 12 . . . .
[9,] . . . . 2 . .
[10,] . . 9 . . . .

Example

M3<−as.matrix(M3)
M3

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] 0 8 0 0 0 0 0
[2,] 0 0 0 8 0 0 0
[3,] 0 0 0 0 3 0 0
[4,] 0 7 0 0 0 0 7
[5,] 0 0 0 0 0 0 0
[6,] 0 0 0 8 0 0 0
[7,] 0 3 0 0 0 0 0
[8,] 0 0 12 0 0 0 0
[9,] 0 0 0 0 2 0 0
[10,] 0 0 9 0 0 0 0

Example4

 Live Demo

i<−c(1,3,8,4,2,7,6,9,1,4,10,5,11,2,12);j<−c(2,5,3,8,6,2,4,2,4,5,2,7,3,2,1);x<−rpois(15,5)
M4<−sparseMatrix(i,j,x=x)
M4
12 x 8 sparse Matrix of class "dgCMatrix"

Output

[1,] . 3 . 3 . . . .
[2,] . 6 . . . 6 . .
[3,] . . . . 2 . . .
[4,] . . . . 3 . . 6
[5,] . . . . . . 6 .
[6,] . . . 6 . . . .
[7,] . 5 . . . . . .
[8,] . . 4 . . . . .
[9,] . 2 . . . . . .
[10,] . 2 . . . . . .
[11,] . . 5 . . . . .
[12,] 6 . . . . . . .

Example

M4<−as.matrix(M4)
M4

Output

 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]  0   3   0   3   0   0    0   0
[2,]  0   6   0   0   0   6    0   0
[3,]  0   0   0   0   2   0    0   0
[4,]  0   0   0   0   3   0    0   6
[5,]  0   0   0   0   0   0    6   0
[6,]  0   0   0   6   0   0    0   0
[7,]  0   5   0   0   0   0    0   0
[8,]  0   0   4   0   0   0    0   0
[9,]  0   2   0   0   0   0    0   0
[10,] 0   2   0   0   0   0    0   0
[11,] 0   0   5   0   0   0    0   0
[12,] 6   0   0   0   0   0    0   0

Example5

 Live Demo

i<−c(1,3,8,4,2,7,6,9,1,4,10,5,11,2,12);j<−c(2,5,3,8,6,2,4,2,4,5,2,7,3,2,1);x<−round(rnorm(15),2)
M5<−sparseMatrix(i,j,x=x)
M5
12 x 8 sparse Matrix of class "dgCMatrix"

Output

[1,] . −0.65 . 0.38 . . . .
[2,] . 0.93 . . . 0.56 . .
[3,] . . . . −1.02 . . .
[4,] . . . . −1.10 . . 0.35
[5,] . . . . . . 1.47 .
[6,] . . . −0.27 . . . .
[7,] . 1.31 . . . . . .
[8,] . . 0.23 . . . . .
[9,] . 1.47 . . . . . .
[10,] . 1.24 . . . . . .
[11,] . . −1.63 . . . . .
[12,] 0.92 . . . . . . .

Example

M5<−as.matrix(M5)
M5

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 0.00 −0.65 0.00 0.38 0.00 0.00 0.00 0.00
[2,] 0.00 0.93 0.00 0.00 0.00 0.56 0.00 0.00
[3,] 0.00 0.00 0.00 0.00 −1.02 0.00 0.00 0.00
[4,] 0.00 0.00 0.00 0.00 −1.10 0.00 0.00 0.35
[5,] 0.00 0.00 0.00 0.00 0.00 0.00 1.47 0.00
[6,] 0.00 0.00 0.00 −0.27 0.00 0.00 0.00 0.00
[7,] 0.00 1.31 0.00 0.00 0.00 0.00 0.00 0.00
[8,] 0.00 0.00 0.23 0.00 0.00 0.00 0.00 0.00
[9,] 0.00 1.47 0.00 0.00 0.00 0.00 0.00 0.00
[10,] 0.00 1.24 0.00 0.00 0.00 0.00 0.00 0.00
[11,] 0.00 0.00 −1.63 0.00 0.00 0.00 0.00 0.00
[12,] 0.92 0.00 0.00 0.00 0.00 0.00 0.00 0.00

Updated on: 08-Feb-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements