Deleet - 1 year ago 49

R Question

I am trying to do sampling of variables for a statistical analysis. I have 10 variables, and I want to examine every possible combination of 5 of them. However, I only want those that follow certain rules. I only want those with 1 xor 2, 3 xor 4, 5 xor 6, 7 xor 8 and 9 xor 10. In other words, all combinations given 5 binary choices (32).

Any idea how to do this efficiently?

A simple idea is to find all the 5 out 10 using:

`library(gtools)`

sets = combinations(10,5) # choose 5 out of 10, all possibilities

sets = split(sets, seq.int(nrow(sets))) #so it's loopable

And then loop over these keeping only the ones that meet the criteria and thus ending up with the 32 ones desired.

But surely there is a more efficient way than this.

Answer Source

This will construct a matrix whose 32 rows enumerate all the possible combinations satisfying your contraint:

```
m <- as.matrix(expand.grid(1:2, 3:4, 5:6, 7:8, 9:10))
## Inspect a few of the rows to see that this works:
m[c(1,4,9,16,25),]
# Var1 Var2 Var3 Var4 Var5
# [1,] 1 3 5 7 9
# [2,] 2 4 5 7 9
# [3,] 1 3 5 8 9
# [4,] 2 4 6 8 9
# [5,] 1 3 5 8 10
```