Skip to content

[Rcpp-devel] Manipulation of IntegerVector

3 messages · Mario Deng, Sameer D'Costa, Sven E. Templer

#
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
#
On Fri, Jul 11, 2014 at 3:19 AM, Mario Deng <mariodeng at googlemail.com>
wrote:
But when I do "matched[j+1] = matched[j]+1;" everything crashes, I don't
I was able to compile and run part of your code on linux outside R Studio.
I did not get any problems with the indexing into the matched variable even
when I changed "matched[j+1] = matched[j] + 1".

I did however get an index error on the line right after. It is where you
set the names variable. After I commented out that line the error goes
away.
IntegerVector matched = match(sorted_vec, one_col);
I had to comment out the line below to avoid an index error. I'm using Rcpp
0.11.2
vectorList[i]=sorted_vec;
Cheers,
Sameer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140711/20943e45/attachment.html>
#
hi,

you should change the line
matched[j+1] = tmp;
to
matched[j-1] = tmp;
to get rid of your error.


require(Rcpp)
require(inline)
sourceCpp(code='
  #include <Rcpp.h>
  using namespace Rcpp;
  // [[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] ){
        matched[j-1] = matched[j]+1;
      }
    }
    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;
}
')

m <- matrix(c(2,3,1),3,1,dimnames=
list(letters[1:3],1))
createVectorList_Numeric_cpp(m)

$`1`
c a b
1 2 3

attr(,"class")
[1] "VectorList" "list"

IMPORTANT:
you will get an error matching names when there are duplicated values.
probably you don't want this...:

m <- matrix(c(2,3,2),3,1,dimnames=list(letters[1:3],1))
createVectorList_Numeric_cpp(m)

$`1`
a a b
2 2 3

attr(,"class")
[1] "VectorList" "list"

Sincerely,
Sven

(+1 for a .sort() that sorts also names...)

@sameer: sorry sending to you twice.
On 11 July 2014 10:19, Mario Deng <mariodeng at googlemail.com> wrote: