[R] Combinations question

Jacques VESLOT jacques.veslot at good.ibl.fr
Wed Aug 9 17:33:05 CEST 2006


library(gtools)
cb <- function(n,r) t(apply(combinations(n, r), 1, function(x) ifelse(1:n %in% x, 1, 0)))
cb(6,3)
-------------------------------------------------------------------
Jacques VESLOT

CNRS UMR 8090
I.B.L (2ème étage)
1 rue du Professeur Calmette
B.P. 245
59019 Lille Cedex

Tel : 33 (0)3.20.87.10.44
Fax : 33 (0)3.20.87.10.31

http://www-good.ibl.fr
-------------------------------------------------------------------


Martin a écrit :
> I need to generate a {0,1} matrix wifht nCr rows and n columns. The rows of the matrix will consist of all possible combinations containing r ones.
> 
> My clumsy attempt for n = 6 and r = 3 is
> 
> X <- expand.grid(c(1,0),c(1,0),c(1,0),c(1,0),c(1,0),c(1,0))
> Y <- X[rowSums(X)==3,]
> 
> I can genralize this in a function but the result is quite ugly. Any suggestions?
> 
> Thank you in advance.
> 
> Martin
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list