Does unique() preserve order?

r

Solution

Here's what I found. This guide leads us to names.c, where we see

{"unique",  do_duplicated,  1,  11, 4,  {PP_FUNCALL, PREC_FN,   0}},

After that we move to unique.c and find an entry

SEXP attribute_hidden do_duplicated(SEXP call, SEXP op, SEXP args, SEXP env)

Browsing the code, we stumble upon

dup = duplicated3(x, incomp, fL, nmax);

which is a reference to

static SEXP duplicated3(SEXP x, SEXP incomp, Rboolean from_last, int nmax)

Finally, the main loop here is

for (i = 0; i < n; i++) {
//      if ((i+1) % NINTERRUPT == 0) R_CheckUserInterrupt();
        v[i] = isDuplicated(x, i, &data);
}

So the answer to my question is yes.

Problem

Imagine we're using the following code: ``` set.seed(42) v <- sample(1:10, 100, T) v <- sort(v) unique.v <- unique(v) ``` Can I be sure that `unique.v` is already sorted? In a more general setting, is that true that `unique()` returns a vector, ordered according to the first entry? The documentation does not imply this, looking to the source with ``` ?unique getAnywhere('unique.default') ``` is not of a much help. Related questions: one, two.

Original source

Related problems