Given are a group indicator variable and some values within groups:
group = rep(c(1,2), each = 3)
val = letters[1:6]
cbind(group, val)
group val
[1,] "1" "a"
[2,] "1" "b"
[3,] "1" "c"
[4,] "2" "d"
[5,] "2" "e"
[6,] "2" "f"
I am looking for a matrix giving me all unique combinations that result from combining one element from each group with one element from each other group. That is, only one element per group is allowed to be ''active'' in each combination.
The desired output is a matrix where each column represents one of the possible combinations. The first four columns of the result matrix may look like this:
[,1] [,2] [,3] [,4]
[1,] 1 0 0 1
[2,] 0 1 0 0
[3,] 0 0 1 0
[4,] 1 1 1 0
[5,] 0 0 0 1
[6,] 0 0 0 0
where the rows corresponds to the rows given in the input matrix above. The first column tells you that a
is active in group 1
and d
is active in group 2
. The second column tells you that b
is active in group 1
and d
is active in group 2
. The third column tells you that c
is active in 1
and d
is active in 2
and so on. Hence, the sum of each column will always be equal to the number of groups, because only one element per group is allowed to be active.
I'm a bit puzzled as to how to obtain the desired output matrix in an organized fashion. I've been thinking of enumerating all possible combinations and restricting to feasible ones (where the sum of the resulting vector elements within groups is exactly equal to one for all groups), but this may cause memory problems for large data sets and I am unsure whether there is a more elegant and efficient approach.
Edit: The solution should generalize to an arbitrary number of groups and an arbitrary number of elements (>1) within groups.
n
groups? Does each group have the same number of values, or could there be different numbers of values per group? – Gregor Thomascbind
automatically - it often tries to create matrices. Notice how yourgroup
numbers are quoted andcharacter
class now. In many cases like this one,data.frame
is preferable.input = data.frame(group, val)
– Gregor Thomas