查找属性变量的组合对 [英] find combination pairs of attribute variables
问题描述
我环顾四周,但找不到确切的解决方案。
I looked around for a solution but could not find an exact one.
给定:
a<-c('a','b','c')
b<-c('d','e','f')
d<-c('g','h')
作为更大玩具的子集集,我希望能够找到
属性(向量)集之间的唯一对。如果我使用
as a toy subset of a much larger set, I want to be able to find unique pairs between attribute (vector) sets. If I use
combn(c(a,b,d),2)
它将返回所有属性元素的所有成对组合。
例如
It would return ALL pairwise combinations of all of the attribute elements. e.g.
combn(c(a,b,d),2)
返回c(a,b)c(a,d)c(a,d)c(a,e)...
returns c(a,b) c(a,d) c(a,d) c(a,e)...
但是我只想要属性之间的成对元素。所以我不会看到a,b或a,c而是
a,da,ea,fb,db,e,b,f等...
But I only want pairs of elements between attributes. So I would not see a,b or a,c but a,d a,e a,f b,d b,e,b,f etc...
我可以使用expand.grid(a,b,d)来做到这一点。
I could sort of do it with expand.grid(a,b,d)..
Var1 Var2 Var3
1 a d g
2 b d g
3 c d g
4 a e g
5 b e g
6 c e g
7 a f g
8 b f g
9 c f g
10 a d h
11 b d h
12 c d h
13 a e h
14 b e h
15 c e h
16 a f h
17 b f h
18 c f h
但现在我有了一组n-col维组合。有什么方法可以将
限制为仅元素对属性,例如combn(x,2)
but now I have an n-col dimensional set of the combinations. Is there any way to limit it to just attribute pairs of elements, such as combn(x,2)
主要目标是找到以下内容的列表:所有属性对之间元素的唯一成对组合,但是我不希望同一属性列中的元素
组合,因为在我的应用程序中是多余的。
The main goal is to find a list of unique pairwise combinations of elements between all attribute pairs, but I do not want combinations of elements within the same attribute column, as it is redundant in my application.
推荐答案
采用网格中每一行的对组合,然后进行过滤以获取唯一条目,我们有:
Taking combinations of pairs in each row in the grid, then filtering to get unique entries, we have this:
unique(do.call(c, apply(expand.grid(a,b,d), 1, combn, m=2, simplify=FALSE)))
返回组合列表:
> L <- unique(do.call(c, apply(expand.grid(a,b,d), 1, combn, m=2, simplify=FALSE)))[1:5]
> length(L) ## 21
> L
## [[1]]
## Var1 Var2
## "a" "d"
##
## [[2]]
## Var1 Var3
## "a" "g"
##
## [[3]]
## Var2 Var3
## "d" "g"
##
## [[4]]
## Var1 Var2
## "b" "d"
##
## [[5]]
## Var1 Var3
## "b" "g"
这篇关于查找属性变量的组合对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!