Re: [R] Combinations

About this list Date view Thread view Subject view Author view

Matthew Wiener (mcw@ln.nimh.nih.gov)
Tue, 14 Dec 1999 09:11:32 -0500 (EST)



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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


About this list Date view Thread view Subject view Author view

This archive was generated by hypermail 2.0b3 on Tue 04 Jan 2000 - 13:34:03 EST