[R] combinations with replications

Jim Lemon jim at bitwrit.com.au
Thu Jul 31 13:19:17 CEST 2008


On Thu, 2008-07-31 at 02:29 -0700, MarinaTarantini wrote:
> Dear all,
> Is there a way to compute and list all combinations with replication of two
> elements in sets of 8 elemnts?
> For example, I've two elements, 0 and 1, and I would to get all possible
> combinations with replication such as, for example, 00000000, 00000001,
> 00000010, and so on. They are 2^8 and it's very hard to list handly!!

Hi Marina,
Here's a Q&D solution for the specific problem that could easily be
extended to different numbers of elements (although it would blow out
very quickly)..

zero8<-rep(0,8)
oneto8<-1:8
cat(zero8,"\n")
for(i in 1:7) {
 combi<-combn(oneto8,i)
 combcol<-dim(combi)[2]
 for(comb in 1:combcol) {
  nextcomb<-zero8
  nextcomb[combi[,comb]]<-1
  cat(nextcomb,"\n")
 }
}
cat(rep(1,8),"\n")

Jim



More information about the R-help mailing list