Skip to content
Prev 13824 / 63424 Next

Ordering of values returned by unique

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