# Sprase Matrix objects in R

In this post, we take a look sparse matrix objects in *R* in terms of object size.

If most of entries in a matrix are 0, we call the matrix is *sparse*. Using sparse matrix object, we can save memory. For example, an Erdös-Rényi graph can be stored as a sparse matrix. The code chunk below is where the edge probability is 0.1, that is, the expected number of edges is \(0.1 {200 \choose 2} = 1990\) out of 19900 pairs.

```
A <- sample_gnp(200,0.1) #From igraph R package
#object size
A %>% as_adj(sparse = T) %>% object.size %>% print # sparse Matrix::dgCMatrix
```

`## 51144 bytes`

`A %>% as_adj(sparse = F) %>% object.size %>% print # dense matrix`

`## 320216 bytes`

We observe that sparse matrix object use much less memory than the usual matrix in R (about 1/6). If there are more zeros, we can save more memory. Figure below displays memory usages of sparse (black) and dense (red) matrix objects to save Erdos-Renyi graph with various edge probability. We observe that sparse matrix requires less memory than usual matrix object.

`sessionInfo()`

```
## R version 4.2.0 (2022-04-22)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Big Sur/Monterey 10.16
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] bench_1.1.2 dplyr_1.0.9 igraph_1.3.1 Matrix_1.4-1 knitr_1.39
##
## loaded via a namespace (and not attached):
## [1] highr_0.9 bslib_0.3.1 compiler_4.2.0 pillar_1.7.0
## [5] jquerylib_0.1.4 tools_4.2.0 digest_0.6.29 tibble_3.1.7
## [9] jsonlite_1.8.0 evaluate_0.15 lifecycle_1.0.1 lattice_0.20-45
## [13] pkgconfig_2.0.3 rlang_1.0.2 cli_3.3.0 rstudioapi_0.13
## [17] yaml_2.3.5 blogdown_1.10 xfun_0.31 fastmap_1.1.0
## [21] stringr_1.4.0 generics_0.1.2 sass_0.4.1 vctrs_0.4.1
## [25] tidyselect_1.1.2 grid_4.2.0 glue_1.6.2 R6_2.5.1
## [29] fansi_1.0.3 rmarkdown_2.14 bookdown_0.26 purrr_0.3.4
## [33] magrittr_2.0.3 htmltools_0.5.2 ellipsis_0.3.2 utf8_1.2.2
## [37] stringi_1.7.6 crayon_1.5.1
```