The values 0 in matrices are very frequent, especially in dummy variables, so in R there is a package called Matrix which allows creating sparse matrices, in other words, matrices that do not use memory when an element’s value is 0.
But skipping the zeros is computationally complex, so it is not always convenient to use sparse matrices. This is shown in the following results where different measurements were made:
Memory Usage

Time in M^2

SVD Decomposition

As you can see, in general it is advisable to use sparse matrices from 40% zeros; before that, it will only use more memory and more CPU.
The code used for the experiment is at: https://github.com/danielfm123/R_matrix_benchmark

Leave a Reply