[R] Combinatorial problem

Bernd Bischl bernd_bischl at gmx.net
Wed Aug 12 13:56:17 CEST 2009


Dimitris Rizopoulos wrote:
> you could try something like the following:
>
> groups <- list(gp1 = 1:3, gp2 = 4:5, gp3 = 6:7,
>                gp4 = 8:10, gp5 = 11)
>
> combn(5, 2, function (x) expand.grid(groups[x]), simplify = FALSE)
> combn(5, 3, function (x) expand.grid(groups[x]), simplify = FALSE)
> combn(5, 4, function (x) expand.grid(groups[x]), simplify = FALSE)
>
and this transforms it nicely into a single matrix

y <- combn(5, 2, function (x) as.matrix(expand.grid(groups[x])), 
simplify = FALSE)
Reduce(rbind, y)


Bernd




More information about the R-help mailing list