[R] Unique arrangements of a vector

Tarmo Remmel tarmo.remmel at utoronto.ca
Mon May 30 21:50:10 CEST 2005

Dear List,

Running on a PC (Windows 2000) with 256 MB RAM, Version R1.9.1

I have a relatively simple problem, which I can solve for relatively small
datasets, but run into difficulties with larger ones.  I believe that my
approach is a hack rather than something elegant and I was hoping that
somebody on this list might help me improve my code.  Basically, given a
vector of values (e.g., 0,0,1,1), I want to generate all of the unique
arrangements of these values, of which there are 4!/(2!2!) = 6.

0 0 1 1
0 1 0 1
0 1 1 0
1 0 0 1
1 0 1 0
1 1 0 0

Using unique() in conjunction with expand.grid(), and later filtering
impossible results, I can obtain the answer.  However, this is slow, and
does not work for large initial vectors and is difficult to filter when
using values beyond 0,1.  Is there some mathematically elegant method for
doing this?  I'd hope to have initial vectors significantly longer than the
demonstrated 4 values (e.g., thousands).

Any help is appreciated and I will gladly SUM afterwards.

Thank you,


Tarmo Remmel  Ph.D.
GUESS Lab, Department of Geography
University of Toronto at Mississauga
Mississauga, Ontario, L5L 1C6
Tel: 905-828-3868
Fax: 905-828-5273
Skype: tarmoremmel

More information about the R-help mailing list