13

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.

Community
  • 1
  • 1
tonytonov
  • 25,060
  • 16
  • 82
  • 98
  • Regarding related question two: [this FAQ](http://stackoverflow.com/questions/19226816/how-can-i-view-the-source-code-for-a-function) will help you more. – Roland Nov 28 '13 at 08:51
  • you want to see `methods(unique)`. specifically `unique.data.frame`, `unique.matrix` – RJ- Nov 28 '13 at 09:10

1 Answers1

19

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.

tonytonov
  • 25,060
  • 16
  • 82
  • 98