title: how to modify values of a file-backed matrix in bigmemory rcpp
body: <p>I am using R bigmemory package and Rcpp to handle big matrices (1 to 10 Million column x 1000 rows). Once I read an interger matrix consisting in 0, 2 and NA into a filebacked bigmemory matrix in R I would like to modify through C++ all the NA values in order to do imputation of the mean values per column or an arbitrary-value-imputation (I show here the latter). </p>
  
  <p>Below is the Rcpp function I have written and that does not work. My hope was that calling <code>BigNA(mybigmatrix@address)</code> from within R could find the elements in the matrix that are NAs and modify its values directly in the backing file. </p>
  
  <p>I think the problem might be in the evaluation of <code>std::isnan(mat[j][i])</code>. I checked this by creating an alternative function that counts the NA values with an accumulator and indeed did not count any NA. But once this is solved, I am also not sure if the expression <code>mat[j][i] = 1</code> would modify the value in the backing file. Writing those statements feels intuitive for me having an R background but might be wrong.</p>
  
  <p>Any help/suggestion would be very much appreciated.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;Rcpp.h&gt;
  #include &lt;bigmemory/MatrixAccessor.hpp&gt;
  #include &lt;numeric&gt;
  // [[Rcpp::depends(BH, bigmemory)]]
  // [[Rcpp::depends(Rcpp)]]
  
  
  // [[Rcpp::export]]
  void BigNA(SEXP pBigMat) {
    /*
    * Imputation of "NA" values for "1" in a big 0, 2 NA matrix.
    */
  
    // Create the external bigmatrix pointer and iniciate matrix accessor
    XPtr&lt;BigMatrix&gt; xpMat(pBigMat);
    MatrixAccessor&lt;int&gt; mat = (*xpMat);
  
    // Iterater over the elements in a matrix and when NA is found, substitute for "1"
    for(int i=0; i&lt; xpMat-&gt;ncol(); i++){
      for(int j=0; j&lt; xpMat-&gt;nrow(); j++){
        if(std::isnan(mat[j][i])){ 
          mat[j][i] = 1;
        }
      }
    }
  } 
  </code></pre>