0

The problem is:

Survey results can be -1 0 or +1

What if there are 10 surveys?

There are 3^10 unique combinations.

But how do I list all possible -1 0 or +1 combinations for n surveys in R?

For example we could get:

-1 0 1 +1 1 0 1 -1 0 1

or

0 0 -1 -1 1 1 -1 0 1 -1

This will give a random sample of 1 possible combinations:

sample(c(-1,0,1),10,replace=T)

But how do I list all possible unique combinations given n surveys?

Thanks

Blckknght
  • 100,903
  • 11
  • 120
  • 169
jc52766
  • 61
  • 4
  • 1
    This is probably a duplicate but you could do it like this: `n <- 3; x <- c(-1, 0, 1); expand.grid(rep(list(x), n))` (for three surveys) – talat Feb 11 '15 at 02:26
  • Thanks docendo discimus, I ran the code and got all of the possible combinations. How can I then decrease this to all possible combinations ignoring order. e.g. so that -1 -1 0 only appears once so that ouput like -1 0 -1 (for example) doesn't also get produced? – jc52766 Feb 19 '15 at 04:40
  • One option would be: `n <- 3; x <- c(-1, 0, 1); dd <- expand.grid(rep(list(x), n)); dd[!duplicated(rowSums(dd)),]`. This would still create all possible combinations first and then remove those rows where the same rowsum occured before. – talat Feb 19 '15 at 12:50

0 Answers0