Search code examples
rrcpp

Rcpp unique order output


I noticed that unique function from Rcpp orders the results

evalCpp("unique(IntegerVector::create(6,6,1,5,5,1))")
[1] 6 5 1
unique(c(6,6,1,5,5,1))
[1] 6 1 5

Is there a way to avoid this ? Thanks for your help


Solution

  • If you look at the (short) source file you see that it uses an internal class IndexHash. I suspect this one sorts by default.

    If original order is paramount, I supposed you could write yourself a new convenience wrapper. It cannot be all this hard: at the risk of wasting a few bytes of memory, allocate a temporary logical vector, use a standard hashmap and loop over the incoming vector. For each value, ask if the hashmap has seen this value, store the boolean answer. Then use that to index the original vector.

    Chances are this is even implemented somewhere. Also look at Armadillo and Eigen for utility functions.