Mario, Your vector matched is 1-indexed, but by passing an element of this vector (match_id) to rownames[] you're treating it as if it's 0-indexed.
-John On Mon, Jul 7, 2014 at 10:33 AM, Mario Deng <[email protected]> wrote: > Hello everyone, > > I am trying to implement a data structure using Rcpp. But I am already > struggling with my first method. > How things (should) work: I want to my function ("createVectorList") a > data frame or matrix. Each column of the matrix gets sorted. This also > holds for the rownames - storing the sorted rownames for each sort column > gives me big memory footprint, but log(n) access while reading later. > Basically everything is fine, but when accessing some data through a for > loop I get "address 0x0, cause 'memory not mapped'". > > Here is my Code: > > #include <Rcpp.h> > using namespace Rcpp; > > // [[Rcpp::export]] > List createVectorList(NumericMatrix df, CharacterVector rownames) { > List sorted_rownames(df.ncol()); > for(int i = 0; i < df.ncol(); i++){ > NumericVector sorted_vec = df(_,i); > std::sort(sorted_vec.begin(), sorted_vec.end()); > NumericVector one_col = df(_,i); > df(_,i)=sorted_vec; > IntegerVector matched = match( sorted_vec, one_col); > CharacterVector one_sorted_row(df.nrow()); > for(int j = 0; j < one_sorted_row.length(); j++){ > int match_id=matched[j]; > String r_name=rownames[match_id]; > one_sorted_row[j]=r_name; > } > sorted_rownames[i]=one_sorted_row; > } > return Rcpp::List::create(Rcpp::Named("Matrix") = df, > Rcpp::Named("rowIDX")=sorted_rownames); > } > > There trouble is within these lines: > > for(int j = 0; j < one_sorted_row.length(); j++){ > int match_id=matched[j]; > String r_name=rownames[match_id]; > one_sorted_row[j]=r_name; > } > > When executing this, I get the error named above. > What I thought about: rownames should be of the type "Proxy" or so, maybe > thats the point? > > Maybe one of you has a hint. > > With beste regards, > > Mario > __ > MD > [email protected] > > > > > _______________________________________________ > Rcpp-devel mailing list > [email protected] > https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel >
_______________________________________________ Rcpp-devel mailing list [email protected] https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
