I wish to generate the unique sequences of elements in a list where some elements are not unique in R
sequence <- c(1,0,1,0)
e.g:
result<-function(sequence)
result:
seq1 seq2 seq3 seq4 seq5 seq6
1 1 1 0 0 0 1
2 0 1 0 1 1 0
3 1 0 1 0 1 0
4 0 0 1 1 0 1
notice that all sequences contain every element from the original sequence, such that the sum of the sequence is always 2
gtools returns "too few different elements"
result <- gtools::permutations(4, 4, coseq)
I am not finding any SO post that directly solve this, but instead allow element repeats:Creating combination of sequences
achievable with expand.grid
and different lengths of sequences.
EDIT: The above is a minimal example, ideally it would work on the sequence:
sequence = c(0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1)
It is somewhat important that the solution does not generate duplicates that are then subsequently removed, since a longer sequence, say 20 or 30 will be very computationally demanding if duplicates are generated.