[R] combinations without repetition
Adaikalavan Ramasamy
ramasamy at cancer.org.uk
Mon Feb 14 03:54:40 CET 2005
This can be simplified slightly by
unique( t( sapply( permn( c(1,1,0) ), "c" ) ) )
Here is another possibility :
a <- expand.grid( 0:1, 0:1, 0:1 )
a[ which( rowSums(a) == 2 ), ]
which gives
Var1 Var2 Var3
4 1 1 0
6 1 0 1
7 0 1 1
Regards, Adai
On Sun, 2005-02-13 at 10:33 -0500, John Fox wrote:
> Dear Marcelo,
>
> This is, I guess, permn() in the combinat package.
>
> How about unique(matrix(unlist(permn(c(1,1,0))), ncol=3, byrow=TRUE)), which
> leaves the permutations in the rows of a matrix?
>
> I hope this helps,
> John
>
> --------------------------------
> John Fox
> Department of Sociology
> McMaster University
> Hamilton, Ontario
> Canada L8S 4M4
> 905-525-9140x23604
> http://socserv.mcmaster.ca/jfox
> --------------------------------
>
> > -----Original Message-----
> > From: r-help-bounces at stat.math.ethz.ch
> > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of
> > mjenny at rumms.uni-mannheim.de
> > Sent: Sunday, February 13, 2005 9:45 AM
> > To: r-help at stat.math.ethz.ch
> > Subject: [R] combinations without repetition
> >
> > The solution is probably simple but I need someone to point me to it.
> > How can I reduce the following 6 permutations to the 3 unique
> > combinations
> > (1,1,0) (1,0,1) (0,1,1)?
> >
> > permn(c(1,1,0))
> > [[1]]
> > [1] 1 1 0
> >
> > [[2]]
> > [1] 1 0 1
> >
> > [[3]]
> > [1] 0 1 1
> >
> > [[4]]
> > [1] 0 1 1
> >
> > [[5]]
> > [1] 1 0 1
> >
> > [[6]]
> > [1] 1 1 0
> >
> > Thanks a lot
> > Marcelo Jenny
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> > http://www.R-project.org/posting-guide.html
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
More information about the R-help
mailing list