[Rcpp-devel] Manipulation of IntegerVector

Mario Deng mariodeng at googlemail.com
Fri Jul 11 10:19:59 CEST 2014


Hello everyone,

I still struggle with Rcpp, I hope you guys are patient.

The problem occurs here:

IntegerVector matched = match(sorted_vec, one_col);
  for(int j = 0; j < (matched.size() - 1); j++){
    if( matched[j] >= matched[j+1] ){
      matched[j+1] = matched[j]+1;
    }
  }

I can access matched[j+1] for reading (tested with Rcout), but when I try to modify it, "matched[j+1] = matched[j]+1" the complete R-Instance crashes.
My first thought was, that there is something wrong with mit indexing. But I can set the elements with a constant int like "matched[j+1] = 1", also I am able to do something like "int foobar = matched[j]+1".

But when I do "matched[j+1] = matched[j]+1;" everything crashes, I don't get any error informations etc. Also, is there a way to avoid that the R instance/RStudio crashes?

With best regards,

Mario

Please find the complete code below.

// [[Rcpp::export]]
List createVectorList_Numeric_cpp(NumericMatrix df) {
  CharacterVector rownames = VECTOR_ELT(df.attr("dimnames"), 0);
  CharacterVector colnames = VECTOR_ELT(df.attr("dimnames"), 1);
  // Check Dimnames
  if (df.nrow() != rownames.size() ) { 
    throw Rcpp::exception("Dimensions and corrosponding names differ in length");
  }
  List vectorList(df.ncol());
  for(int i = 0; i < df.ncol(); i++){
    NumericVector sorted_vec = df(_,i);
    sorted_vec=sorted_vec.sort();
    NumericVector one_col = df(_,i);
    IntegerVector matched = match(sorted_vec, one_col);
    for(int j = 0; j < (matched.size() - 1); j++){
      if( matched[j] >= matched[j+1] ){
        const int tmp = matched[j]+1;
        matched[j+1] = tmp;
      }
    }
    sorted_vec.attr("names")=rownames[matched-1];
    vectorList[i]=sorted_vec;
  }
  vectorList.attr("names") = colnames;
  CharacterVector classIDs = CharacterVector::create("VectorList", "list");
  vectorList.attr("class") = classIDs;
  return vectorList;
}
__
MD
mariodeng at googlemail.com






More information about the Rcpp-devel mailing list