Skip to content

permutation of vectors (1 or 0)

7 messages · olga30dec, arun, Jeffrey Dick +3 more

#
Dear all,

I have 2 vectors of 10 elements, each of them contains either 0 or 1, like
following:
[1] 0 0 0 0 0 0 0 0 0 0
[1] 1 1 1 1 1 1 1 1 1 1

How can I obtain a matrix rows of which can take all possible combinations?

e.g.
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0
...
0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 1 1
 1 1 1 1 1 1 1 1 1 1

Many thanks,
Olga




--
View this message in context: http://r.789695.n4.nabble.com/permutation-of-vectors-1-or-0-tp4653607.html
Sent from the R help mailing list archive at Nabble.com.
#
HI,

library(gtools)
If you need ?combinations()
combinations(2,10,0:1,repeats.allowed=TRUE)
#????? [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
?#[1,]??? 0??? 0??? 0??? 0??? 0??? 0??? 0??? 0??? 0???? 0
?#[2,]??? 0??? 0??? 0??? 0??? 0??? 0??? 0??? 0??? 0???? 1
?#[3,]??? 0??? 0??? 0??? 0??? 0??? 0??? 0??? 0??? 1???? 1
?#[4,]??? 0??? 0??? 0??? 0??? 0??? 0??? 0??? 1??? 1???? 1
?#[5,]??? 0??? 0??? 0??? 0??? 0??? 0??? 1??? 1??? 1???? 1
?#[6,]??? 0??? 0??? 0??? 0??? 0??? 1??? 1??? 1??? 1???? 1
?#[7,]??? 0??? 0??? 0??? 0??? 1??? 1??? 1??? 1??? 1???? 1
?#[8,]??? 0??? 0??? 0??? 1??? 1??? 1??? 1??? 1??? 1???? 1
?#[9,]??? 0??? 0??? 1??? 1??? 1??? 1??? 1??? 1??? 1???? 1
#[10,]??? 0??? 1??? 1??? 1??? 1??? 1??? 1??? 1??? 1???? 1
#[11,]??? 1??? 1??? 1??? 1??? 1??? 1??? 1??? 1??? 1???? 1
#or permutations()
permutations(2,10,0:1,repeats.allowed=TRUE)
A.K.






----- Original Message -----
From: olga30dec <olga at herenstraat.nl>
To: r-help at r-project.org
Cc: 
Sent: Thursday, December 20, 2012 7:52 AM
Subject: [R] permutation of vectors (1 or 0)

Dear all,

I have 2 vectors of 10 elements, each of them contains either 0 or 1, like
following:
[1] 0 0 0 0 0 0 0 0 0 0
[1] 1 1 1 1 1 1 1 1 1 1

How can I obtain a matrix rows of which can take all possible combinations?

e.g.
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0
...
0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 1 1
1 1 1 1 1 1 1 1 1 1

Many thanks,
Olga




--
View this message in context: http://r.789695.n4.nabble.com/permutation-of-vectors-1-or-0-tp4653607.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
#
On 12/20/2012 02:13 PM, arun wrote:
Thanks, this is exactly what I need.

Regards,
Olga
#
you might also try (names modified a bit since R already has a NULL object)

        
On Thu, Dec 20, 2012 at 9:24 PM, Olga Lyashevska <olga at herenstraat.nl> wrote:
#
On 12/20/2012 02:13 PM, arun wrote:
This does what I need, but if I increase a number of permutations (59
instead of 10), then I get an error.

Error: cannot allocate vector of size 1.5 Gb

Is there a smart way of increasing the maximum number of permutations
that R can handle?


Cheers,
Olga
#
> you might also try (names modified a bit since R already has a NULL object)
    >> Zeros <- rep(0,10)
    >> Ones <- rep(1,10)
    >> expand.grid(Map(c, Zeros, Ones))

Wow -- really neat!

Thank you, Jeff
#
use the 64-bit version of R and get at least 4e18 bytes of memory.
Since that is quite a bit, you might want to use another approach
since you would need that much disk to just store the result which
might take 1.3 million cpu hours to calculate.
On Thu, Dec 20, 2012 at 9:07 AM, Olga Lyashevska <olga at herenstraat.nl> wrote: