[Rcpp-devel] Manipulation of IntegerVector
Sameer D'Costa
sameerdcosta at gmail.com
Fri Jul 11 14:21:02 CEST 2014
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
> get any error informations etc. Also, is there a way to avoid that the R
> instance/RStudio crashes?
>
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.
>
> // [[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;
> }
> }
>
I had to comment out the line below to avoid an index error. I'm using Rcpp
0.11.2
> 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;
> }
>
>
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>
More information about the Rcpp-devel
mailing list