[R] More efficient alternative to combn()?

Søren Højsgaard Soren.Hojsgaard at agrsci.dk
Sat Mar 27 15:04:26 CET 2010


Perhaps slightly off topic but there is a faster alternative to combn() called combnPrim() in the gRbase package:

> library(gRbase)
> system.time({for (ii in 1:10000) combn(1:6,3)})
   user  system elapsed 
   4.02    0.00    4.02 
> system.time({for (ii in 1:10000) combnPrim(1:6,3)})
   user  system elapsed 
   0.28    0.00    0.28 

Notice that combnPrim() does not take the FUN argument but can easily be extended to do so.
regards
Søren


________________________________________
Fra: r-help-bounces at r-project.org [r-help-bounces at r-project.org] På vegne af Charles C. Berry [cberry at tajo.ucsd.edu]
Sendt: 26. marts 2010 18:26
Til: elgorgonzola
Cc: r-help at r-project.org
Emne: Re: [R] More efficient alternative to combn()?

On Fri, 26 Mar 2010, elgorgonzola wrote:

>
> Hi,
>
> i am working on a problem where i need to compute the products of all
> possible combinations of size m of the elements of a vector. I know that
> this can be achieved using the function combn(), e.g.:
>
>> vector <- 1:6
>> combn(x = vector, m = 3, FUN = function(y) prod(y))
>
> In my case the vector has 2000 elements and i need to compute the values
> specified above for m = 32. Using combn() i encounter problems for m >= 4.
> Is there any alternative to combn() that works more efficiently? Also my
> vector contains many duplicates, so there are actually only about 300
> distinct values in the vector. This reduces the number of possible
> combinations significantly. Is there any way to use this fact to reduce the
> computational cost?

It doesn't matter what the computational cost is.

Even if the problem was just to enumerate the choose(300,32) products of
unique elements you would need more than 10^33 Gigabytes of memory to
store the result.

Unless there is a lot more structure in your problem than you have
described or unless some form of approximation is acceptable, it is not a
problem you can hope to solve.

HTH,

Chuck

>
> Thanks in advance,
>
> El
> --
> View this message in context: http://n4.nabble.com/More-efficient-alternative-to-combn-tp1692117p1692117.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.
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu               UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901

______________________________________________
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.


More information about the R-help mailing list