How to generate all possible combinations of vectors without caring for order?

后端 未结 2 1735
再見小時候
再見小時候 2020-11-27 22:44

In a data frame, I have one column containing character strings. Let\'s say it looks like this:

x <- unique(df[,1])
x
\"A\" \"A\" \"B\" \"B\" \"B\" \"C\"
         


        
相关标签:
2条回答
  • 2020-11-27 23:19

    I think you are looking for combn:

    x <- c("A", "A", "B", "B", "B", "C")
    combn(x,2)
    

    Gives:

         [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
    [1,] "A"  "A"  "A"  "A"  "A"  "A"  "A"  "A"  "A"  "B"   "B"   "B"   "B"   "B"   "B"  
    [2,] "A"  "B"  "B"  "B"  "C"  "B"  "B"  "B"  "C"  "B"   "B"   "C"   "B"   "C"   "C"  
    

    And if you want only unique values in x (I have no idea why you have duplicate values in x in the first place if it's the result of a unique() call):

    > combn(unique(x),2)
         [,1] [,2] [,3]
    [1,] "A"  "A"  "B" 
    [2,] "B"  "C"  "C" 
    
    0 讨论(0)
  • 2020-11-27 23:24

    There's the combn function in the utils package:

    t(combn(LETTERS[1:3],2))
    #      [,1] [,2]
    # [1,] "A"  "B" 
    # [2,] "A"  "C" 
    # [3,] "B"  "C"
    

    I'm a little confused as to why your x has duplicated values.

    0 讨论(0)
提交回复
热议问题