[Rcpp-devel] Negative vector lengths from rep_each()

Barth Riley barthriley at comcast.net
Mon Oct 29 15:12:10 CET 2018


Dear list

I have written the following function to produce multidimensional arrays using Rcpp:

NumericVector arrayFromVector(NumericVector input, IntegerVector dim) { 
  
  // check for zero-length vectors
  if(dim.length() == 0 || input.length() == 0) {
    return NULL;
  }
  
  long p = 1;
  for(int i = 0; i < dim.length(); i++) {
    p *= dim[i];
  }
  
  if((input.length() == 1) & (p > 1)) {
   	 input = Rcpp::rep_each(input, p);
  } else if (input.length() != p) {
    return NULL;
  } 
  
  input.attr("dim") = dim;
  return input;
}


This function usually produces the desired result, but in some cases (it seems with relatively large vector lengths) I get the error “negative length vectors are not allowed.” The error occurs on the line:

    input = Rcpp::rep_each(input, p);

I changed p from an int to a long, thinking I was encountering an overflow issue. But this did not resolve the problem. The test vector contains 45,000 values.

Any suggestions?

Thanks in advance

Barth


Sent from Mail for Windows 10

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20181029/df5d26f5/attachment.html>


More information about the Rcpp-devel mailing list