Dear all,
I have 2 vectors of 10 elements, each of them contains either 0 or 1, like
following:
Null<-rep(0,10)
Null
[1] 0 0 0 0 0 0 0 0 0 0
One<-rep(1,10)
One
[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:
Null<-rep(0,10)
Null
[1] 0 0 0 0 0 0 0 0 0 0
One<-rep(1,10)
One
[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.
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
Jeffrey Dick <j3ffdick at gmail.com>
on Thu, 20 Dec 2012 21:40:15 +0800 writes:
> 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:
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