[R] Unique arrangements of a vector

Uwe Ligges ligges at statistik.uni-dortmund.de
Tue May 31 09:05:25 CEST 2005


Tarmo Remmel wrote:

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

This one is quite outdated...

> 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).

Nice for length 4, but you will get problems far sooner than for length 
1000... please calculate the size before!
For thing as short as 4, you might want to try out permutations() in 
package "gtools" (formerly in bundle gregmisc, since yesterday a single 
package).

Uwe Ligges


> Any help is appreciated and I will gladly SUM afterwards.
> 
> Thank you,
> 
> Tarmo
> 
> __________________________________________
> 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
> http://eratos.erin.utoronto.ca/remmelt
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list