0

An easy one for many of you, I'm sure, which will save my day : I need to generate a permutation set of all the pairs of a sequence of numbers. For example, for 1:6, it will give as a final result, 30 subsets, i.e. n(n-1) :

(1,2),(3,4),(5,6)
...
(1,6),(2,3),(4,5)

I need pairs, not couples, so that (3,4) and (4,3) is an unique pair.

combn(1:6,2) gives me a table with my pairs as columns, but how do I produce my list of pairs out of it?

combn(1:6,2)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
[1,]    1    1    1    1    1    2    2    2    2     3     3     3     4     4     5
[2,]    2    3    4    5    6    3    4    5    6     4     5     6     5     6     6

Thank you

  • thats the solution I believe @markus. – Andre Elrico Aug 16 '18 at 11:07
  • Thats an general solution to such problems when you dont have a simplify option: `lapply(data.frame(combn(1:6,2)),I)`. of course markus solution hits right on! – Andre Elrico Aug 16 '18 at 11:10
  • You should check out [this answer](https://stackoverflow.com/a/47983855/4408538) to the question [R: Permutations and combinations with/without replacement and for distinct/non-distinct items/multiset](https://stackoverflow.com/q/22569176/4408538) – Joseph Wood Aug 16 '18 at 14:25

2 Answers2

2

We can set the argument simplify = FALSE in combn such that it returns a list:

combn(1:6, 2, simplify = FALSE)
#[[1]]
#[1] 1 2
#
#[[2]]
#[1] 1 3
#
#[[3]]
#[1] 1 4
#
#[[4]]
#[1] 1 5
#...
markus
  • 25,843
  • 5
  • 39
  • 58
  • The OP wants both 3,4 and 4,3 but if I'm right, your suggestion only gives 3,4. – milan Aug 16 '18 at 11:43
  • @milan Thanks for the comment but I am not sure about it. As OP writes "for 1:6, it will give as a final result, 15 subsets", and continues "`combn(1:6,2)` gives me a table with my pairs". We let him take a look. – markus Aug 16 '18 at 11:49
  • 1
    This is it! About "pairs", I was not clear : (3,4) and (4,3) is the same object, so only one of them should appear. Thanks to both of two. – Philippe Ramirez Aug 16 '18 at 12:15
  • Well, after looking at it, it's not really what I expected. I must get at the end, in a form or another, a list of series of unique pairs : a((1,2,),(3,5),(4,6)), b((2,4),(1,3),(5,6) and so on. – Philippe Ramirez Aug 16 '18 at 13:35
  • @PhilippeRamirez That's a good question. Please consider to post a new one here on the forum. I am sure somebody will provide the answer you're looking for quickly. – markus Aug 16 '18 at 21:07
1

Repeat your procedure in the reversed order and rbind. For example, now both c(3,4) and c(4,3) are included. I'm using t to transpose, which makes it easier to view the data.

rbind( t(combn(1:6,2)), t(combn(6:1,2)) )

      [,1] [,2]
 [1,]    1    2
 [2,]    1    3
 [3,]    1    4
 [4,]    1    5
 [5,]    1    6
 [6,]    2    3
 [7,]    2    4
 [8,]    2    5
 [9,]    2    6
[10,]    3    4
[11,]    3    5
[12,]    3    6
[13,]    4    5
[14,]    4    6
[15,]    5    6
[16,]    6    5
[17,]    6    4
[18,]    6    3
[19,]    6    2
[20,]    6    1
[21,]    5    4
[22,]    5    3
[23,]    5    2
[24,]    5    1
[25,]    4    3
[26,]    4    2
[27,]    4    1
[28,]    3    2
[29,]    3    1
[30,]    2    1
milan
  • 4,782
  • 2
  • 21
  • 39