1

Is there a way to generate all the unique sets of the following permutations, where I am able to change N and R easily.

library(gtools)
x <- c("A","B","C","D")
x <- permutations(n=4,r=2,v=x)
x
    [,1] [,2]
[1,] "A"  "B" 
[2,] "A"  "C" 
[3,] "A"  "D" 
[4,] "B"  "A" 
[5,] "B"  "C" 
[6,] "B"  "D" 
[7,] "C"  "A" 
[8,] "C"  "B" 
[9,] "C"  "D" 
[10,] "D"  "A" 
[11,] "D"  "B" 
[12,] "D"  "C"

For example sets 1 and 4 are not unique, AB and BA contain the same characters.

The following list is unique, and this is what I want.

    [,1] [,2]
[1,] "A"  "B" 
[2,] "A"  "C" 
[3,] "A"  "D" 
[4,] "B"  "C"
[5,] "B"  "D"
[6,] "C"  "D"
Aaron Walton
  • 150
  • 1
  • 10

1 Answers1

3

conbn would give you what you need:

 #combn gives you the combinations, t is only used to transpose the matrix
 t(combn(x, 2))
#     [,1] [,2]
#[1,] "A"  "B" 
#[2,] "A"  "C" 
#[3,] "A"  "D" 
#[4,] "B"  "C" 
#[5,] "B"  "D" 
#[6,] "C"  "D"
LyzandeR
  • 37,047
  • 12
  • 77
  • 87