0

I wanted to get all the combinations for 73,000 choose 2 and I tried to use combn in order to calculate it.

combn(73000,2)

I received the following error:

Error in matrix(r, nrow = len.r, ncol = count) :
invalid 'ncol' value (too large or NA)

I figured that the number of combinations is 2,664,463,500 so multiplied by 8 should yield around 22GB which I had free on my machine. So even though it's a lot of combinations, it shouldn't fail. Any alternative way to calculate the number of combinations or explanations of why combn fails?

ornit
  • 125
  • 1
  • 1
  • 5
  • 1
    Have a look at https://stackoverflow.com/a/47983855/4408538 near the bottom. – Joseph Wood Feb 05 '21 at 00:12
  • Thank you! I missed that when I was looking for a solution. Do you know any alternatives to cbind as well? After I was successfully able to get 2 vectors I was willing to use them as coordinates for indexing a matrix. At the end instead of coordinates indexing I resorted to use linear indexing.. But I would love to know if there is a way to do it – ornit Feb 05 '21 at 11:58

1 Answers1

0

I dug into the code and apparently when constructing the output matrix the dimensions are converted to integer:

count <- as.integer(round(choose(n, m)))
out <- matrix(r, nrow = len.r, ncol = count) # matrix for now

Eliminating the as.integer from count increases its range and in my case it doesn't overflow anymore. It wasn't enough though, I continue to receive the same error again. I couldn't find a way to initialize the matrix as type integer, so I created two vectors instead (m=2)like that:

col1 <- vector(mode="integer", length=n)
col2 <- vector(mode="integer", length=n)

With few more adjustments it runs now.

I hope this might help to others as well.

ornit
  • 125
  • 1
  • 1
  • 5