Search code examples
rloopscombinationstournament

combine one element with all other elements of a different column


I do have two pairs of players in each group (a1 - a8) and I want to combine the elements of one matchup with the second matchup to get every combination for each person with the other matchup.

#groups a1-a8 with two matchups

$a1
     [,1]     [,2]    
[1,] "Paul"   "Stefan"
[2,] "Markus" "Andre" 

$a2
     [,1]     [,2]   
[1,] "Julian" "Lupo" 
[2,] "Jo"     "Peter"
... 

so I need something like this..

matchups for a1
   
$Paul
              [,1]     [,2]    
        [1,] "Paul"   "Paul"
        [2,] "Stefan" "Andre" 

$Markus
             [,1]     [,2]    
        [1,] "Markus" "Markus"
        [2,] "Stefan" "Andre" 
       
$Stefan  
             [,1]     [,2]    
        [1,] "Stefan" "Stefan"
        [2,] "Paul"   "Markus" 
   
$Andre
             [,1]     [,2]    
        [1,] "Andre"  "Andre"
        [2,] "Paul"   "Markus" 

the same should be possible for each group (a2 -> a8)

How can I solve that problem?


Solution

  • The following code should do the trick (just replace a1 in all places if you want other matchups): split(expand.grid(a=a1[,1], b=a1[,2]), a1[,1])