如何在不关心订单的情况下生成所有可能的向量组合?

时间:2021-02-25 21:33:42

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"

I'd like to get all possible combinations of the unique character strings as sets of 2 without caring about their order, so A, B is the same as B, A, and I don't want to get same values as combination like A, A. So far, I got until this point:

我想得到所有可能的独特字符串组合作为2组,而不关心他们的顺序,所以A,B与B,A相同,我不希望获得与A组合相同的值,A。到目前为止,我到目前为止:

comb <- expand.grid(x, x)
comb <- comb[which(comb[,1] != comb[,2]),]

But this still leaves the problem of having rows with the same combination of strings in a different order. How do I get rid of this?

但是这仍然存在以不同顺序具有相同字符串组合的行的问题。我怎么摆脱这个?

2 个解决方案

#1


16  

There's the combn function in the utils package:

utils包中有combn函数:

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.

我有点困惑为什么你的x有重复的值。

#2


12  

I think you are looking for combn:

我想你正在寻找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):

如果你只想要x中的唯一值(我不知道你为什么在x中首先有重复值,如果它是unique()调用的结果):

> combn(unique(x),2)
     [,1] [,2] [,3]
[1,] "A"  "A"  "B" 
[2,] "B"  "C"  "C" 

#1


16  

There's the combn function in the utils package:

utils包中有combn函数:

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.

我有点困惑为什么你的x有重复的值。

#2


12  

I think you are looking for combn:

我想你正在寻找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):

如果你只想要x中的唯一值(我不知道你为什么在x中首先有重复值,如果它是unique()调用的结果):

> combn(unique(x),2)
     [,1] [,2] [,3]
[1,] "A"  "A"  "B" 
[2,] "B"  "C"  "C"