[Rcpp-devel] sort() not sorting names

Sven E. Templer sven.templer at gmail.com
Fri Jun 27 12:14:06 CEST 2014

sorting an Rcpp Vector leaves it's name attribute unsorted.
See this code:

f <- cxxfunction(sig=c(xx="integer"), plugin="Rcpp", body='
IntegerVector x(xx);
return x;')
x <- c(a=1, b=2, c=0)

I would love to have this available, but I am not so shure how to
extend inst/include/Rcpp/vector/Vector.h - For a solution I would also
suggest to at least replace the names with NA? But 'this' is not very
familiar to me. So can it be done with:

337     Vector& sort(){
 338         typename traits::storage_type<RTYPE>::type* start =
internal::r_vector_start<RTYPE>( Storage::get__() ) ;
 339         std::sort(
 340             start,
 341             start + size(),
 342             internal::NAComparator<typename
traits::storage_type<RTYPE>::type >()
 343         ) ;
+++         int n = this.size();
+++         CharacterVector new( n, NA_STRING );
+++         this.names() = new;
 344         return *this ;
 345     }

Of course I'd prefer a it sorting the names.

Thank you.

More information about the Rcpp-devel mailing list