Re: [R] combinations with replications

From: Jim Lemon <jim_at_bitwrit.com.au>
Date: Thu, 31 Jul 2008 21:19:17 +1000

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

R-help_at_r-project.org 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. Received on Thu 31 Jul 2008 - 11:19:42 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 31 Jul 2008 - 12:33:01 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.