[R] all possible subsets of r out of n

Wed Aug 28 21:51:10 CEST 2002

```I had the same question a couple of years ago and here is the reply I got
from Tim Hesterberg of Insightful Corp. It should work in R.

Ravi.

----------------------------------------------------------------------------
--
I don't have that.  But there is code to compute all combinations of
k integers out of n that you might adapt.
For a recursive version, see Venables & Ripley's subsets() function
(page 50 of their S Programming book).
A non-recursive version (harder to understand and extend, but faster
for larger n because it avoids recomputing quantities) is:

combinations _ function(n, k){
# Compute all n choose k combinations of size k from 1:n
# Return matrix with k rows and choose(n,k) columns.
# Avoids recursion.
if(!is.numeric(n) || length(n) != 1 || n%%1) stop("'n' must be an
integer")
if(!is.numeric(k) || length(k) != 1 || k%%1) stop("'k' must be an
integer")
if(k > n || k <= 0) return(numeric(0))
rowMatrix _ function(n) structure(1:n, dim=c(1,n))
colMatrix _ function(n) structure(1:n, dim=c(n,1))
if(k == n) return(colMatrix(n))
if(k == 1) return(rowMatrix(n))
L _ vector("list", k)
# L[[j]] will contain combinations(N, j) for N = 2:n
L[[1]] _ rowMatrix(2)
L[[2]] _ colMatrix(2)
Diff _ n-k
for(N in seq(3, n, by=1)){
# loop over j in reverse order, to avoid overwriting
for(j in seq(min(k, N-1), max(2, N-Diff), by= -1))
L[[j]] _ cbind(L[[j]], rbind(L[[j-1]], N, deparse.level=1))
if(N <= Diff+1) L[[1]] _ rowMatrix(N)
else L[[N-(Diff+1)]] _ numeric(0)
if(N <= k) L[[N]] _ colMatrix(N)
}
L[[k]]
}
----- Original Message -----
From: "Yuelin Li" <yuelin at mail.med.upenn.edu>
To: <r-help at stat.math.ethz.ch>
Sent: Wednesday, August 28, 2002 12:28 PM
Subject: [R] all possible subsets of r out of n

> Has anyone written a function that returns all possible
> subsets of r elements out of a vector that contains n
> elements?  I have not been able to find an answer in the FAQ
> or the archives.
>
> Say I have a vector: letters[1:10].  I want to get a matrix
> of 120 rows that contains all possible subsets of 3 letters
> out of the 10.  And I would like to be able to generalize to
> any r out of n, like function(x=letters[1:10], r=3) that
> returns:
>
> a, b, c
> a, b, d
> a, b, e
> ...
>
> etc.
>
>
> - Yuelin Li.
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-.-
> 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 at stat.math.ethz.ch
>
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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 at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```