From owner-r-help@stat.math.ethz.ch Tue Dec 14 13:03:13 1999 Received: from stat.math.ethz.ch (majordom@hypatia.ethz.ch [129.132.58.23]) by t2.mscf.uky.edu (8.8.5/8.8.5) with ESMTP id NAA09810 for ; Tue, 14 Dec 1999 13:03:10 -0500 (EST) Received: by stat.math.ethz.ch (8.9.1/8.9.1) id SAA14023 for r-help-gang-use; Tue, 14 Dec 1999 18:15:59 +0100 (MET) Received: (from daemon@localhost) by stat.math.ethz.ch (8.9.1/8.9.1) id SAA14017 for ; Tue, 14 Dec 1999 18:15:58 +0100 (MET) Received: from sophie(129.132.58.2), claiming to be "sophie.ethz.ch" via SMTP by hypatia, id smtpdAAAa003Qx; Tue Dec 14 18:15:50 1999 Received: (maechler@localhost) by sophie.ethz.ch (8.6.12/D-MATH-client) id SAA02889 for r-help@stat.math.ethz.ch; Tue, 14 Dec 1999 18:15:48 +0100 Received: (from daemon@localhost) by stat.math.ethz.ch (8.9.1/8.9.1) id PAA08253 for ; Tue, 14 Dec 1999 15:12:23 +0100 (MET) Received: from ln.nimh.nih.gov(156.40.248.2) via SMTP by hypatia, id smtpdAAAa0020p; Tue Dec 14 15:12:14 1999 Received: from localhost (mcw@localhost) by ln.nimh.nih.gov (980427.SGI.8.8.8/950213.SGI.AUTOCF) via SMTP id JAA24196; Tue, 14 Dec 1999 09:11:32 -0500 (EST) Date: Tue, 14 Dec 1999 09:11:32 -0500 (EST) From: Matthew Wiener To: a s cc: r-help@stat.math.ethz.ch Subject: Re: [R] Combinations In-Reply-To: <385226496.945109840240.JavaMail.root@web07.pub01> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-r-help@stat.math.ethz.ch Precedence: bulk Status: OR Alex: Bill Venables sent me these functions in response to a similar query of mine a few months ago (you can find the full reply by searching the R archives under the heading "avoiding loops, gaining generality"): subsets <- function(r, n, v = 1:n) if(r <= 0) NULL else if(r >= n) v[1:n] else rbind(cbind(v[1], Recall(r - 1, n - 1, v[-1])), Recall(r, n - 1, v[-1])) permutations <- function(n, v = 1:n) { if(n == 1) return(v[1]) X <- NULL for(i in 1:n) X <- rbind(X, cbind(v[i], permutations(n - 1, v[-i]))) X } Hope this helps. Matt On Mon, 13 Dec 1999, a s wrote: > I need some help here. > >From a vector of variable length (say, c(A,B,C,D), I need to obtain all > posible combinations (order doesn't matter) of the elements. > I would like a function such as: > function(x,c(A,B,C,D)) > to give a matrix (for x=3) like: > A B C > A B D > A C D > B C D > > or for x=2 > A B > A C > A D > B C > B D > C D > > And so on. > Any ideas?? > > Thank you, > Alex Ahgarin > Data Management > I.R.W. Co. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._