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: x2
Hi. 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. ______________________________________________ R-help@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.