-1

I am trying to generate all possible permutations of a long vector (30 elements) consisting of 0 and 1. I have tried several functions including permn and expand.grid , but none of them seems to be able to cope with this issue (due to size constraints).

Any advice would be really helpful!

Boris
  • 1
  • What R version are you on? – user2357112 Jun 26 '17 at 18:38
  • `permutations()` from [gtools](https://cran.rstudio.com/web/packages/gtools/index.html)? – Dan Jun 26 '17 at 18:43
  • Are you saying you want all `2^30` possible options? Or do you have a set number of 1s and 0s and want to keep that constant? – Dason Jun 26 '17 at 18:47
  • If you could answer my question and provide some more details on *why* you want to do this we may be able to provide better suggestions about how to solve your actual problem. – Dason Jun 26 '17 at 18:55
  • @user2357112 I am using the latest one – Boris Jun 26 '17 at 19:02
  • @Lyngbakr thank you! permutations () is almost perfect match for me – Boris Jun 26 '17 at 19:03
  • @Dason I have a vector of 1's and 0's of length 30. There are say 14 elements "1", the remaining ones are "0". I want to obtain a matrix, where each row would represent a unique combination of 1's and 0's such that sum of each row is always equal to 14 – Boris Jun 26 '17 at 19:06
  • @dww It returns an error : cannot allocate vector of length 1073741824 – Boris Jun 26 '17 at 19:29

2 Answers2

1

What you need is a multiset permutation.

> library(iterpc)
> I <- iterpc(c(16, 14), labels=c(0,1), ordered=TRUE)
> getlength(I)
[1] 145422675

The matrix is 145422675 by 30, too large to be stored in a single matrix. Use getnext to get the next 10000000 permutations.

> getnext(I, 10000000)

You could repeat the above 15 times in order to get all the permutations.

Randy Lai
  • 3,084
  • 2
  • 22
  • 23
0

a simple and naive implementation...

system.time(x <- combn(30,14))
# user  system elapsed 
# 252.42    2.17  254.98 

pryr::object_size(x)
# 8.14 GB

dim(x)
# [1]        14 145422675

system.time(
  apply(x[,1:1e6],2,function(index){
    V <- rep(0,30)
    V[index] <- 1
    return(V)
  } )
)
# user  system elapsed 
# 8.23    0.24    8.52 

I would not run the entire x unless there is a good reason...

platypus
  • 516
  • 3
  • 8