[Rd] Ordering of values returned by unique

Witold Eryk Wolski wolski at molgen.mpg.de
Wed Sep 29 17:17:54 CEST 2004


Hi,

Is the ordering of the values returned something on what I can rely on, 
a form of a standard,  that a function called unique in R (in futher 
versions) will return the uniq elements in order of they first occurcence.

 > x<-c(2,2,1,2)
 > unique(x)
[1] 2 1

Its seems not to be the standard. E.g. matlab
 >> x=[2,2,1,2]
x =
     2     2     1     2
 >> unique(x)
ans =
     1     2

I just noted it because, the way how it is working now is extremely 
usefull for some applications (e.g tree traversal), so i use it in a 
script. But I am a little woried if I can rely on this behaviour in 
further versions. And furthermore can I assume that someone reading the 
code will think that it works in that way?
Or is it better to define a additional function?
keeporderunique<-function(x)
{
    res<-rep(NA,length(unique(x))
    count<-0
    for(i in x)
    {
        if(!i%in%res)
            {
                    count<-count+1
                     res[count]<-i
            }  
    }
    res
}

/E



-- 
Dipl. bio-chem. Witold Eryk Wolski         
MPI-Moleculare Genetic
Ihnestrasse 63-73 14195 Berlin           _
tel: 0049-30-83875219                   'v'
http://www.molgen.mpg.de/~wolski       /   \
mail: witek96 at users.sourceforge.net  ---W-W----
      wolski at molgen.mpg.de



More information about the R-devel mailing list