[Rcpp-devel] Manipulation of IntegerVector
Sven E. Templer
sven.templer at gmail.com
Fri Jul 11 15:08:21 CEST 2014
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:
> 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
>
>
>
>
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
More information about the Rcpp-devel
mailing list