[R] Permutations

Marc Schwartz MSchwartz at MedAnalytics.com
Tue Jul 13 21:39:01 CEST 2004


On Tue, 2004-07-13 at 14:29, Marc Schwartz wrote:
> On Tue, 2004-07-13 at 14:07, Jordi Altirriba GutiÃ©rrez wrote:
> > Dear R users,
> > Im a beginner user of R and Ive a problem with permutations that I dont 
> > know how to solve. Ive 12 elements in blocks of 3 elements and I want only 
> > to make permutations inter-blocks (no intra-blocks) (sorry if the 
> > terminology is not accurate), something similar to:
> > 
> > 1 2 3 | 4 5 6 | 7 8 9 | 10 11 12   ----------1st permutation
> > 
> > 1 3 2 | 4 5 6 | 7 8 9 | 10 11 12   NO
> >    -  -
> > 3 2 1 | 4 5 6 | 7 8 9 | 10 11 12   NO
> > -  -  -
> > 1 2 4 | 3 5 6 | 7 8 9 | 10 11 12   YES-----2nd permutation
> >       -    -
> > 4 5 6 | 1 2 3 | 7 8 9 | 10 11 12   YES-----3rd permutation
> > -  -  -   -  -  -
> > 4 5 6 | 2 1 3 | 7 8 9 | 10 11 12   NO
> >            -  -
> > ....
> 
> 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)


Ack....one correction. The use of matrix() here was actually redundant.

You can use:

> permutations(4, 4, groups)
      [,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"  

Sorry about that.

Marc




More information about the R-help mailing list