Re: [R] combinatorics

From: Ted Harding <Ted.Harding_at_nessie.mcc.ac.uk>
Date: Fri 13 Oct 2006 - 16:40:24 GMT


On 13-Oct-06 Robin Hankin wrote:

> Hi
> 
> How do I generate all ways of ordering  sets of indistinguishable
> items?
> 
> suppose I have two A's, two B's and a C.
> 
> Then I want
> 
> AABBC
> AABCB
> AACBC ---->> I think you mean AACBB here!
> ABABC
> . . .snip...
> BBAAC
> . . .snip...
> CBBAA
> 
> [there are 5!/(2!*2!) = 30 arrangements.  Note AABBC != BBAAC]
> 
> How do I do this?

I've tried to think of an efficient and economical (and therefore clever) way of doing this for larger problems; but that will have to wait for another day!

Meanwhile, a problem of the order of the one you describe above can be solved quite slickly:

X<-c("A","A","B","B","C")
library(combinat)

##[result below stripped of " quotes for clarity] unique(array(permn(X)))
[[1]]

 [1]   A A B B C

[[2]]
[1] A A B C B
[[3]]
[1] A A C B B
[[4]]
[1] A C A B B
[[5]]
[1] C A A B B
[[6]]
[1] A B A B C
[[7]]
[1] A B A C B
[[8]]
[1] A B C A B

[[9]]
 [1]   A C B A B

[[10]]
 [1]   C A B A B

[[11]]
 [1]   C B A A B

[[12]]
 [1]   B C A A B

[[13]]
 [1]   B A C A B

[[14]]
 [1]   B A A C B

[[15]]
 [1]   B A A B C

[[16]]
 [1]   B A B A C

[[17]]
 [1]   B A B C A

[[18]]
 [1]   B A C B A

[[19]]
 [1]   B C A B A

[[20]]
 [1]   C B A B A

[[21]]
 [1]   C A B B A

[[22]]
 [1]   A C B B A

[[23]]
 [1]   A B C B A

[[24]]
 [1]   A B B C A

[[25]]
 [1]   A B B A C

[[26]]
 [1]   B B A A C

[[27]]
 [1]   B B A C A

[[28]]
 [1]   B B C A A

[[29]]
 [1]   B C B A A

[[30]]

 [1] C B B A A

However, the above simple function will quickly get short of breath if the total number of items gets much above, say 10.

Hoping this helps!
Ted.



E-Mail: (Ted Harding) <Ted.Harding@nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861
Date: 13-Oct-06                                       Time: 17:40:20
------------------------------ XFMail ------------------------------

______________________________________________
R-help@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 and provide commented, minimal, self-contained, reproducible code. Received on Sat Oct 14 04:15:30 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 13 Oct 2006 - 19:30:09 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.