Message-ID: <Pine.A41.4.44.0306080847270.136656-100000@homer03.u.washington.edu>
Date: 2003-06-08T16:01:33Z
From: Thomas Lumley
Subject: Ordering long vectors
In-Reply-To: <Pine.LNX.4.44.0306081255340.7226-100000@tal.stat.umu.se>
On Sun, 8 Jun 2003, [ISO-8859-1] Göran Broström wrote:
> On Sat, 7 Jun 2003, Göran Broström wrote:
>
> >
> > I need to order a long vector of integers with rather few unique values.
> > This is very slow:
> >
> > > x <- sample(rep(c(1:10), 50000))
> > > system.time(ord <- order(x))
> > [1] 189.18 0.09 190.48 0.00 0.00
> >
> > But with no ties
> >
> > > y <- sample(500000)
> > > system.time(ord1 <- order(y))
> > [1] 1.18 0.00 1.18 0.00 0.00
> >
> > it is very fast!
> > This gave me the following idea: Since I don't care about keeping the
> > order within tied values, why not add some small disturbance to x,
Another option:
> system.time(a<-sapply(sort(unique(x)),function(i) which(x==i)))
This turns out to be slightly slower than your method, but doesn't require
that you know what the smallest difference between values is (and works
for characters as well as numbers)
-thomas