Variance of function combn

111 views Asked by At

I have been using the marvellous function combn from the package utils. This function creates all the possible combinations without repetition Combn description. I am going to introduce one use of the function that I need to do, but it is not defined in the combn function. I would love to introduce it with a nice example. Nevertheless, the actual goal that we want to achieve is more complicated and it will be performed with more data.

We want to play a game that only 3 people can play. However we are 4 people. We want to know all the possible groups for playing the game. The names of the participants are Alex, David, John and Zoe. The possible combinations would be:

names <- c("Alex","David","John","Zoe")
people.per.group <- 3
combn(names,people.per.group)

#Output
     [,1]    [,2]    [,3]   [,4]   
[1,] "Alex"  "Alex"  "Alex" "David"
[2,] "David" "David" "John" "John" 
[3,] "John"  "Zoe"   "Zoe"  "Zoe"  

However, we have problems because Alex does not have a good relationship with John. So that, we do not want to include both of them in the group. So that we can create groups for the people that we can only chose one of them.

# Alex   --> Group 1
# David  --> Group 2
# John   --> Group 1
# Zoe    --> Group 3
only.one.per.group <- c(1,2,1,3)

I am looking for a function that allow me to do the same as combn but limiting the combinations by the variable only.one.per.group. Naming the function that I am looking forward to use var.combn, the code would be:

names <- c("Alex","David","John","Zoe")
only.one.per.group <- c(1,2,1,3)
people.per.group <- 3
var.combn(names,people.per.group,only.one.per.group)

#Output
       [,1]     [,2]      
[1,]  "Alex"   "David"
[2,]  "David"  "John" 
[3,]  "Zoe"    "Zoe"  

I have been looking for a similar function for quite a long time. So, it would be super useful if you could tell me a function for doing it, or any way that pops into your mind to do it.

I hope that you enjoyed the example and I would love to get any ideas of how to do it.

2

There are 2 answers

0
Alexandra Thayer On

Here is a different way of doing it

Find all possible group combinations:

names <- c("Alex","David","John","Zoe")
x <- expand.grid(names, names, names)

All possible groups:

    Var1  Var2  Var3
1   Alex  Alex  Alex
2  David  Alex  Alex
3   John  Alex  Alex
4    Zoe  Alex  Alex
5   Alex David  Alex
6  David David  Alex
7   John David  Alex
8    Zoe David  Alex
9   Alex  John  Alex
10 David  John  Alex
11  John  John  Alex
12   Zoe  John  Alex
13  Alex   Zoe  Alex
14 David   Zoe  Alex
15  John   Zoe  Alex
16   Zoe   Zoe  Alex
17  Alex  Alex David
18 David  Alex David
19  John  Alex David
20   Zoe  Alex David
21  Alex David David
22 David David David
23  John David David
24   Zoe David David
25  Alex  John David
26 David  John David
27  John  John David
28   Zoe  John David
29  Alex   Zoe David
30 David   Zoe David
31  John   Zoe David
32   Zoe   Zoe David
33  Alex  Alex  John
34 David  Alex  John
35  John  Alex  John
36   Zoe  Alex  John
37  Alex David  John
38 David David  John
39  John David  John
40   Zoe David  John
41  Alex  John  John
42 David  John  John
43  John  John  John
44   Zoe  John  John
45  Alex   Zoe  John
46 David   Zoe  John
47  John   Zoe  John
48   Zoe   Zoe  John
49  Alex  Alex   Zoe
50 David  Alex   Zoe
51  John  Alex   Zoe
52   Zoe  Alex   Zoe
53  Alex David   Zoe
54 David David   Zoe
55  John David   Zoe
56   Zoe David   Zoe
57  Alex  John   Zoe
58 David  John   Zoe
59  John  John   Zoe
60   Zoe  John   Zoe
61  Alex   Zoe   Zoe
62 David   Zoe   Zoe
63  John   Zoe   Zoe
64   Zoe   Zoe   Zoe

Find groups that satisfy conditions:

 x <- x[which(x[,1] != x[,2]),]
    x <- x[which(x[,1] != x[,3]),]
    x <- x[which(x[,2] != x[,3]),]
    x <- x[-which((x[,1] == "Alex" & x[,2] == "John")),]
    x <- x[-which((x[,1] == "Alex" & x[,3] == "John")),]
    x <- x[-which((x[,2] == "Alex" & x[,3] == "John")),]
    x <- x[-which((x[,2] == "Alex" & x[,1] == "John")),]
    x <- x[-which((x[,3] == "Alex" & x[,1] == "John")),]
    x <- x[-which((x[,3] == "Alex" & x[,2] == "John")),]

Result:

    Var1  Var2  Var3
8    Zoe David  Alex
14 David   Zoe  Alex
20   Zoe  Alex David
28   Zoe  John David
29  Alex   Zoe David
31  John   Zoe David
40   Zoe David  John
46 David   Zoe  John
50 David  Alex   Zoe
53  Alex David   Zoe
55  John David   Zoe
58 David  John   Zoe
0
ekstroem On

Here's a fairly compact solution based on an assumption that exactly one individual from each group should be picked (and this assumption may not always be true - it was a bit unclear from the OP). It uses the fact that one individual from each group should be selected, so if we add the names to the groups and use expand.grid() then we have the solution right away.

DF <- data.frame(names=c("Alex","David","John","Zoe"),
                 groups=c(1, 2, 1, 3),
                 stringsAsFactors =FALSE)
expand.grid(lapply(unique(DF$groups), function(i) {DF$names[which(DF$groups==i)]}))

This produces

  Var1  Var2 Var3
1 Alex David  Zoe
2 John David  Zoe

which were the two combinations you were after. A slightly more compact solution (still using base R) would be

expand.grid(by(DF, DF$groups, function(x) x$names))

which is perhaps easier to read.

It also works with more complicated groupings:

DF <- data.frame(names=c("Alex","David","John","Zoe", "Bob", "Charles"),
                 groups=c(1, 2, 1, 3, 2, 3),
                 stringsAsFactors =FALSE)

expand.grid(by(DF, DF$groups, function(x) x$names))

which produces

  Var1  Var2    Var3
1 Alex David     Zoe
2 John David     Zoe
3 Alex   Bob     Zoe
4 John   Bob     Zoe
5 Alex David Charles
6 John David Charles
7 Alex   Bob Charles
8 John   Bob Charles

Now if you want to pick a less than one from each group then the code above should be wrapped and applied over the results produces by combn().