Dear All, I'm wondering if there is a R function could give me all the combinations of the grouping/cluster result, given the number of the groups. e.g. 3 objects: x1 x2 x3, number of groups is 2 so the result will be group1:x1,x2; group2: x3 group1: x1;group2: x2,x3 group1: x1,x3;group2: x2 many thanks yan
On Tue, Jan 24, 2012 at 05:19:42PM +0000, yan jiao wrote:> Dear All, > > I'm wondering if there is a R function could give me all the > combinations of the grouping/cluster result, given the number of the groups. > e.g. > 3 objects: x1 x2 x3, number of groups is 2 > so the result will be > group1:x1,x2; group2: x3 > group1: x1;group2: x2,x3 > group1: x1,x3;group2: x2Hi. Represent a grouping by a mapping, which assigns a group number to each element. In your example, we will have the groupings x1 x2 x3 1 1 2 1 2 2 1 2 1 This may be generated as expand.grid(x1=1, x2=1:2, x3=1:2) where we have to eliminate the first row with all ones. expand.grid(x1=1, x2=1:2, x3=1:2)[ -1, ] x1 x2 x3 2 1 2 1 3 1 1 2 4 1 2 2 For 4 elements, we get gr <- expand.grid(x1=1, x2=1:2, x3=1:2, x4=1:2)[ -1, ] row.names(gr) <- 1:nrow(gr) gr x1 x2 x3 x4 1 1 2 1 1 2 1 1 2 1 3 1 2 2 1 4 1 1 1 2 5 1 2 1 2 6 1 1 2 2 7 1 2 2 2 For example, group 1 in grouping 3 may be obtained names(gr)[gr[3, ] == 1] [1] "x1" "x4" and group 2 is names(gr)[gr[3, ] == 2] [1] "x2" "x3" For more groups, a similar approach may be used. Eliminating equivalent groupings may be done, for example, by requiring that if an element is assigned to group i, then each of the groups 1, ..., i-1 is assigned to some earlier element. So, 1, 2, 3, 2 is OK, but 1, 3, 2, 2 is not. Petr Savicky.
On Jan 25, 2012, at 12:10 PM, yan wrote:> thanks petr, > what if I got 200 elements, so I have to write expand.grid(x1=1, > x2=1:2, > x3=1:3, x4=1:3, x5=1:3....x200=1:3))?Perhaps same thing that will happen when those monks finish the Towers of Hanoi? 2*3^198 [1] 5.902533e+94 -- David Winsemius, MD West Hartford, CT
You will run out of memory. 2*3^198 is about 5e95 rows and 4GB of RAM can probably only hold about 5e8 rows. --------------------------------------------------------------------------- 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...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. yan <y.jiao at ucl.ac.uk> wrote:>thanks petr, >what if I got 200 elements, so I have to write expand.grid(x1=1, >x2=1:2, >x3=1:3, x4=1:3, x5=1:3....x200=1:3))? > >Many thanks > >yan > >-- >View this message in context: >http://r.789695.n4.nabble.com/function-for-grouping-tp4324436p4327812.html >Sent from the R help mailing list archive at Nabble.com. > >______________________________________________ >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.
On Wed, Jan 25, 2012 at 09:10:13AM -0800, yan wrote:> thanks petr, > what if I got 200 elements, so I have to write expand.grid(x1=1, x2=1:2, > x3=1:3, x4=1:3, x5=1:3....x200=1:3))?Hi. As others told you, the problem is not in reformulating the command in a concise way, but in enormous memory and time requirements of constructing all these partitionings. Try to formulate further conditions, which the required partitionings should satisfy. This can reduce their number or allow a different approach to solve your problem than listing all partitionings. Petr Savicky.
I nominate this response for the fortunes package. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of David Winsemius > Sent: Wednesday, January 25, 2012 10:23 AM > To: yan > Cc: r-help at r-project.org > Subject: Re: [R] function for grouping > > > On Jan 25, 2012, at 12:10 PM, yan wrote: > > > thanks petr, > > what if I got 200 elements, so I have to write expand.grid(x1=1, > > x2=1:2, > > x3=1:3, x4=1:3, x5=1:3....x200=1:3))? > > Perhaps same thing that will happen when those monks finish the Towers > of Hanoi? > > 2*3^198 > [1] 5.902533e+94 > > -- > > David Winsemius, MD > West Hartford, CT > > ______________________________________________ > 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.