Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
460 views
in Technique[技术] by (71.8m points)

R combn fails when asked for large number of combinations (even though memory should suffice)

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?

question from:https://stackoverflow.com/questions/66050972/r-combn-fails-when-asked-for-large-number-of-combinations-even-though-memory-sh

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...