[R] Combinations with two part column

Gabor Grothendieck ggrothendieck at gmail.com
Tue May 17 19:00:18 CEST 2005


On 5/17/05, Sofyan Iyan <sofyan.iyan at gmail.com> wrote:
> Dear R-helpers,
> I am a beginner using R.
> This is the first question in this list.
> My question, Is there possible to make combinations with two part column?
> If I have a number 1,2,3,4,5,6,7,8. I need the result something like below:
> 
> 1,2,3,4,5     6,7,8
> 1,2,3,4,7     5,6,8
> 2,3,4,5,6     1,7,8
> 1,2,3,6,7     4,5,8
> 1,2,3,4,8     5,6,7
> 3,4,6,7,8     1,2,5
> ....
> 

Try this:

library(gtools)
t(apply(combinations(8,5), 1, function(x) c(x,setdiff(1:8, x))))




More information about the R-help mailing list