Does unique() preserve order?

后端 未结 1 1046
温柔的废话
温柔的废话 2021-02-20 07:08

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

相关标签:
1条回答
  • 2021-02-20 07:59

    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.

    0 讨论(0)
提交回复
热议问题