0

I have a very large sparse matrix of size

150000x2500

and in this matrix I have some cells whose values are NA. So I want to assign 0 to those NA cells. Currently I am doing

x[is.na(x)] <- 0

where x is the matrix. But this approach is taking a huge time. Is there any better way to do it.

tanay
  • 468
  • 5
  • 16
  • that looks to be the fastest way to do it in base R. I think there are some R packages that speed up sparse matrices, try the `Matrix`, or `slam` packages (`glmnet` if you're doing GLMs) - have a search on CRAN. The other thing to consider is why the elements of `x` are becoming `NA` in the first place - perhaps there is some way to avoid this. Another option - some methods have a `na.rm` parameter to ignore NAs, so depending on what you're doing you could look up the help file to see if there's some way to ignore the NAs, negating the need to replace them. – mathematical.coffee Apr 08 '14 at 03:12
  • See also [this question](http://stackoverflow.com/questions/1167448/most-mature-sparse-matrix-package-for-r) on options for sparse matrices in R. – mathematical.coffee Apr 08 '14 at 03:15
  • 1
    Maybe you're running out of memory, or `x` is not a matrix (e.g., a data.frame)? This takes about 18s for me (this seems long, but not 'huge'; the matrix itself is 2.8Gb). – Martin Morgan Apr 08 '14 at 03:43
  • ya...i m running out of memory sometimes... – tanay Apr 08 '14 at 03:56

0 Answers0