# Re: [R] Permutations

From: Rolf Turner <rolf_at_math.unb.ca>
Date: Wed 14 Jul 2004 - 06:02:25 EST

Marc Schwartz wrote (in response to a question from Jordi Altirriba):

> You can use the permutations() function in the 'gregmisc' package on
> CRAN:
>
> # Assuming you installed 'gregmisc' and used library(gregmisc)
> # First create 'groups' consisting of the four blocks
> groups <- c("1 2 3", "4 5 6", "7 8 9", "10 11 12")
>
> # Now create a 4 column matrix containing the permutations
> # The call to permutations() here indicates the number of blocks in
> # groups (4), the required length of the output (4) and the vector of
> # elements to permute
> perms <- matrix(permutations(4, 4, groups), ncol = 4)
>
> > perms
> [,1] [,2] [,3] [,4]
> [1,] "1 2 3" "10 11 12" "4 5 6" "7 8 9"
> [2,] "1 2 3" "10 11 12" "7 8 9" "4 5 6"
> [3,] "1 2 3" "4 5 6" "10 11 12" "7 8 9"
> [4,] "1 2 3" "4 5 6" "7 8 9" "10 11 12"
> [5,] "1 2 3" "7 8 9" "10 11 12" "4 5 6"
> [6,] "1 2 3" "7 8 9" "4 5 6" "10 11 12"
> [7,] "10 11 12" "1 2 3" "4 5 6" "7 8 9"
> [8,] "10 11 12" "1 2 3" "7 8 9" "4 5 6"
> [9,] "10 11 12" "4 5 6" "1 2 3" "7 8 9"
> [10,] "10 11 12" "4 5 6" "7 8 9" "1 2 3"
> [11,] "10 11 12" "7 8 9" "1 2 3" "4 5 6"
> [12,] "10 11 12" "7 8 9" "4 5 6" "1 2 3"
> [13,] "4 5 6" "1 2 3" "10 11 12" "7 8 9"
> [14,] "4 5 6" "1 2 3" "7 8 9" "10 11 12"
> [15,] "4 5 6" "10 11 12" "1 2 3" "7 8 9"
> [16,] "4 5 6" "10 11 12" "7 8 9" "1 2 3"
> [17,] "4 5 6" "7 8 9" "1 2 3" "10 11 12"
> [18,] "4 5 6" "7 8 9" "10 11 12" "1 2 3"
> [19,] "7 8 9" "1 2 3" "10 11 12" "4 5 6"
> [20,] "7 8 9" "1 2 3" "4 5 6" "10 11 12"
> [21,] "7 8 9" "10 11 12" "1 2 3" "4 5 6"
> [22,] "7 8 9" "10 11 12" "4 5 6" "1 2 3"
> [23,] "7 8 9" "4 5 6" "1 2 3" "10 11 12"
> [24,] "7 8 9" "4 5 6" "10 11 12" "1 2 3"

This does not solve the problem that was posed. It only permutes the blocks, and does not allow for swapping between blocks. For instance it does produce the ``acceptable'' permutation

1 2 4 | 3 5 6 | 7 8 9 | 10 11 12 YES-----2nd permutation

I would guess that a correct solution is likely to be pretty difficult. I mean, one ***could*** just generate all 12! permutations of 1 to 12 and filter out the unacceptable ones. But this is getting unwieldy (12! is close to half a billion) and is inelegant. And the method does not ``generalize'' worth a damn.

cheers,

```					Rolf Turner
rolf@math.unb.ca

______________________________________________
```
R-help@stat.math.ethz.ch mailing list