In a function, I want to calculate numeric values, give names to them and return a sorted NumericVector
in Rcpp. I can sort the vectors (using this), but the order of the names of the values remains the same.
library(Rcpp)
x <- c(a = 1, b = 5, c = 3)
cppFunction('
NumericVector foo(NumericVector x) {
std::sort(x.begin(), x.end());
return(x);
}')
foo(x)
## a b c
## 1 3 5
I want the function to return this:
## a c b
## 1 3 5
Is it possible? How can I achieve this?