[R] exaustive subgrouping or combination
Bert Gunter
gunter.berton at gene.com
Wed Jun 27 23:55:34 CEST 2007
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.
More information about the R-help
mailing list