title: how can I replace a matrix element of NaN with 0 in R
body: <p>I have a matrix that looks like this</p>
  
  <pre><code>a = matrix(c(5,10,NaN,NaN,4,5,3,NaN,NaN,2), nr=5)
  a
  
       [,1] [,2]
  [1,]    5    5
  [2,]   10    3
  [3,]  NaN  NaN
  [4,]  NaN  NaN
  [5,]    4    2
  </code></pre>
  
  <p>Now I want to have those <code>NaN</code>'s replaced by zeros. I know <code>is.nan</code> function will return the indices of those elements, but how can I make use of the fact that if a <code>NaN</code> exists in a row, then all elements of that row are all <code>NaN</code>'s (as in the example above, rows 3 and 4). Thanks!</p>