I am hoping to create all possible permutations of a vector containing two different values, in which I control the proportion of each of the values.
For example, if I have a vector of length three and I want all possible combinations containing a single 1, my desired output is a list looking like this:
list.1 <- list(c(1,0,0), c(0,1,0), c(0,0,1))
In contrast, if I want all possible combinations containing three 1s, my desired output is a list looking like this:
list.3 <- list(c(1,1,1))
To put it another way, the pattern of the 1
and 0
values matter, but all 1
s should be treated as identical to all other 1
s.
Based on searching here and elsewhere, I've tried several approaches:
expand.grid(0:1, 0:1, 0:1) # this includes all possible combinations of 1, 2, or 3 ones
permn(c(0,1,1)) # this does not treat the ones as identical (e.g. it produces (0,1,1) twice)
unique(permn(c(0,1,1))) # this does the job!
So, using the function permn
from the package combinat
seems promising. However, where I scale this up to my actual problem (a vector of length 20, with 50% 1s and 50% 0s, I run into problems:
unique(permn(c(rep(1,10), rep(0, 10))))
# returns the error:
Error in vector("list", gamma(n + 1)) :
vector size specified is too large
My understanding is that this is happening because, in the call to permn
, it makes a list containing all possible permutations, even though many of them are identical, and this list is too large for R to handle.
Does anyone have a suggestion for how to work around this?
Sorry if this has been answered previously - there are many, many SO questions containing similar language but different problems and I have not bene able to find a solution which meets my needs!