How do I change all nonzero values to 1 while using an object of class big.matrix
? If I convert to normal matrix, the object size is over 2gb (10^7 columns, 100 rows), so this is not feasible.
Thanks!
You can use Rcpp to do that (put this code in an .cpp file and source it with Rcpp::sourceCpp
):
// [[Rcpp::depends(BH, bigmemory)]]
#include <bigmemory/MatrixAccessor.hpp>
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
void to_one(SEXP bm_addr) {
XPtr<BigMatrix> xptr(bm_addr);
MatrixAccessor<double> macc(*xptr);
for (size_t j = 0; j < macc.ncol(); j++)
for (size_t i = 0; i < macc.nrow(); i++)
if (macc[j][i] != 0) macc[j][i] = 1;
}
/*** R
library(bigmemory)
r <- 100
c <- 10000
bm <- matrix(sample(0:4, r * c, replace = TRUE), r, c)
bm <- as.big.matrix(bm, type = "double")
bm[, 1]
to_one(bm@address)
bm[, 1]
*/