Reputation: 627
I looked around for a solution but could not find an exact one.
Given:
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)
returns c(a,b) c(a,d) c(a,d) c(a,e)...
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...
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
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.
Upvotes: 2
Views: 3966
Reputation: 89057
First, create a list where each element is a pair of your original vectors, e.g. list(a, b)
:
L <- list(a, b, d)
L.pairs <- combn(seq_along(L), 2, simplify = FALSE, FUN = function(i)L[i])
Then run expand.grid
for each of these pairs and put the pieces together:
do.call(rbind, lapply(L.pairs, expand.grid))
# Var1 Var2
# 1 a d
# 2 b d
# 3 c d
# [...]
# 19 d h
# 20 e h
# 21 f h
Upvotes: 1
Reputation: 42629
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)))
A list of combinations is returned:
> 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"
Upvotes: 2