将一个元素与不同列的所有其他元素组合在一起/ R

时间:2017-07-21 10:40:40

标签: r loops combinations two-columns tournament

我在每组中都有两对球员(a1 - a8),我想将一场比赛的元素与第二场比赛相结合,以便为每个人与另一场比赛获得每个组合。

#groups a1-a8 with two matchups

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

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

所以我需要这样的东西..

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" 

对于每个组(a2 - > a8)

应该是可能的

有人知道如何解决这个问题到目前为止我无法解决这个问题。很多

1 个答案:

答案 0 :(得分:0)

以下代码应该可以解决问题(如果你想要其他匹配,只需在所有地方替换a1): split(expand.grid(a=a1[,1], b=a1[,2]), a1[,1])