[Rcpp-devel] sort() not sorting names

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


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


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


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.
Sven


More information about the Rcpp-devel mailing list