0

My problem is the following. I need to generate the combinations of binary outcomes for a sequence of 27 draws, i.e. 000000000000000000000000000, 000000000000000000000000001, ..., 111111111111111111111111111.

In R I would do this with expand.grid

result <- expand.grid(rep(list(0:1), 27)) #Error, object too large for workspace
# 2^27 = 134217728 unique combinations
# write.table(result, "result.csv", row.names=F)

My ultimate goal is to save the resulting object for later use.

Is there a way to iteratively compute the entries of the result object and save it by appending?

Any idea how I can achieve this?

JBJ
  • 866
  • 9
  • 21
  • 1
    Why in the world would you want to do this? Note these are the same as the binary expressions of the numbers 0 through 2^27-1, so you can follow the methods of http://stackoverflow.com/q/6614283/1756702. No need to actually generate them all. – A. Webb Mar 02 '16 at 18:00
  • Do you really need to keep the whole object in memory? – sus_mlm Mar 02 '16 at 18:04
  • see also: http://stackoverflow.com/questions/12088080/how-to-convert-number-into-binary-vector. There are several ways to get binary expression from a number. – fishtank Mar 02 '16 at 18:08
  • the `iterators` package might be useful. – Ben Bolker Mar 02 '16 at 18:56

1 Answers1

0

You could also use std::bitset in C++ (see Changing integer to binary string of digits).

In toBinString.cpp:

#include <Rcpp.h>
#include <bitset>
using namespace Rcpp;
// [[Rcpp::export]]
std::string toBinString(int x){
  std::string s1 = std::bitset< 27 >(x).to_string();
  return s1;
}

In R, execute:

> library(Rcpp)
> sourceCpp(file="toBinString.cpp")
> sapply(1:4,toBinString)
[1] "000000000000000000000000001" "000000000000000000000000010"
[3] "000000000000000000000000011" "000000000000000000000000100"
> toBinString(2^27-1)
[1] "111111111111111111111111111"
> strsplit(toBinString(2^27-1),"")
[[1]]
 [1] "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1"
[20] "1" "1" "1" "1" "1" "1" "1" "1"

Alternatively, do everything in R using cppFunction:

cppFunction('std::string toBinString(int x)
  { return(std::bitset<27>(x).to_string()); }',
  includes="#include <bitset>")
Community
  • 1
  • 1
fishtank
  • 3,718
  • 1
  • 14
  • 16