Dear Colleagues, I am looking for a package or previous implemented R to subgroup and exaustively divide a vector of squence into 2 groups. For example: 1, 2, 3, 4 I want to have a group of 1, (2,3,4) (1,2), (3,4) (1,3), (2,4) (1,4), (2,3) (1,2,3), 4 (2,3), (1,4) ... Can someone help me as how to implement this? I get some imaginary problem when the sequence becomes large. Thanks much in advance. -- Waverley @ Palo Alto [[alternative HTML version deleted]]
Do you realize that for n items, there are 2^(n-1) such groups -- since you essentially want all possible subsets divided by 2: all possible subsets and their complements repeats each split twice, backwards and forwards. So this will quickly become ummm... rather large. If you really want to do this, one lazy but inefficient way I can think of is to use expand.grid() to generate your subsets. Here's a toy example that shows you how with n = 4. ## generate a list with four components each of which ## is c(TRUE,FALSE) -- note that a data.frame is a list z <- data.frame(matrix(rep(c(TRUE,FALSE),4),nrow=2) ## Now use expand.grid to get all 2^4 possible 4 vectors as rows ix <- do.call(expand.grid,z) ## This is essentially what you want. apply(ix[1:8,],1,function(x)(1:4)[x]) ## gives you the list of first splits, while apply(ix[16:9],... gives the complements (note reversal of order). Bert Gunter Genentech Nonclinical Statistics -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Waverley Sent: Wednesday, June 27, 2007 1:57 PM To: r-help at stat.math.ethz.ch Subject: [R] exaustive subgrouping or combination Dear Colleagues, I am looking for a package or previous implemented R to subgroup and exaustively divide a vector of squence into 2 groups. For example: 1, 2, 3, 4 I want to have a group of 1, (2,3,4) (1,2), (3,4) (1,3), (2,4) (1,4), (2,3) (1,2,3), 4 (2,3), (1,4) ... Can someone help me as how to implement this? I get some imaginary problem when the sequence becomes large. Thanks much in advance. -- Waverley @ Palo Alto [[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.
On Wed, 27 Jun 2007, Waverley wrote:> Dear Colleagues, > > I am looking for a package or previous implemented R to subgroup and > exaustively divide a vector of squence into 2 groups. > > For example: > > 1, 2, 3, 4 > > I want to have a group of > 1, (2,3,4) > (1,2), (3,4) > (1,3), (2,4) > (1,4), (2,3) > (1,2,3), 4 > (2,3), (1,4) > ... > > Can someone help me as how to implement this?help.search("combinations") ?combn Have you read the posting guide mentioned at the bottom of each message on the list?> I get some imaginary problem > when the sequence becomes large. > > Thanks much in advance. > > -- > Waverley @ Palo Alto > > [[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. >--------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...2k
> Waverley <waverley.paloalto at gmail.com> asked: > > Dear Colleagues, > > I am looking for a package or previous implemented R to subgroup and > exaustively divide a vector of squence into 2 groups. > > -- > Waverley @ Palo AltoGoogle "[R] Generating all possible partitions" and you will find some R code from 2002 or so. David Duffy. -- | David Duffy (MBBS PhD) ,-_|\ | email: davidD at qimr.edu.au ph: INT+61+7+3362-0217 fax: -0101 / * | Epidemiology Unit, Queensland Institute of Medical Research \_,-._/ | 300 Herston Rd, Brisbane, Queensland 4029, Australia GPG 4D0B994A v