Reduce the size of a large matrix filled with only a few kinds of elements in R

I have a matrix sdist

in the R

following way.

library("babynames")
library("stringdist")
d <- babynames
sdist <- stringdistmatrix(d$name[1:1000], d$name[1001:10000], method="lv", useBytes = T)
sdist[sdist > 3] <- Inf

      

The size of this matrix is โ€‹โ€‹large, even if there are only a few element types.

object.size(sdist)
72000112 bytes

dim(sdist)
[1] 1000 9000
length(sdist)
[1] 9000000

table(sdist)
sdist
      0       1       2       3     Inf 
   4093   12125   72937  381084 8529761 

      

Is there a way to reduce the size of these types of matrices. I cannot use sparse matrices since the number 0s is less.

+3


source to share





All Articles