<!DOCTYPE HTML PUBLIC "-//W3C//DTD W3 HTML//EN">
<HTML>
<HEAD>
<META content=text/html;charset=iso-8859-1 http-equiv=Content-Type><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<META content='"MSHTML 4.72.3110.7"' name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#d8d0c8>
<DIV><FONT color=#000000 size=2>There is a recursive function called
"subsets" in the S-Programming book by V&R, that lets you generate
all unique combinations of n objects taken k at a time. Tim Hesterberg
also has a non-recursive function called "combinations", which may
also be used. Note that the recursive function uses a lot more memory. You
can use any of these functions to generate unique, exhaustive samples.
</FONT></DIV>
<DIV><FONT color=#000000 size=2></FONT><FONT size=2></FONT> </DIV>
<DIV><FONT size=2></FONT><FONT color=#000000 size=2>Hope this
helps,</FONT></DIV>
<DIV><FONT color=#000000 size=2></FONT><FONT size=2>Ravi.</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>Here is the non-recursive function (courtesy: Tim
Hesterberg):</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV>combinations _ function(n, k){<BR> # Compute all n choose k
combinations of size k from 1:n<BR> # Return matrix with k rows and
choose(n,k) columns.<BR> # Avoids recursion.<BR> if(!is.numeric(n)
|| length(n) != 1 || n%%1) stop("'n' must be an integer")<BR>
if(!is.numeric(k) || length(k) != 1 || k%%1) stop("'k' must be an
integer")<BR> if(k > n || k <= 0) return(numeric(0))<BR>
rowMatrix _ function(n) structure(1:n, dim=c(1,n))<BR> colMatrix _
function(n) structure(1:n, dim=c(n,1))<BR> if(k == n)
return(colMatrix(n))<BR> if(k == 1) return(rowMatrix(n))<BR> L _
vector("list", k)<BR> # L[[j]] will contain combinations(N, j)
for N = 2:n<BR> L[[1]] _ rowMatrix(2)<BR> L[[2]] _
colMatrix(2)<BR> Diff _ n-k<BR> for(N in seq(3, n,
by=1)){<BR> # loop over j in reverse order, to avoid
overwriting<BR> for(j in seq(min(k, N-1), max(2, N-Diff), by=
-1))<BR> L[[j]] _ cbind(L[[j]], rbind(L[[j-1]], N,
deparse.level=0))<BR> if(N <= Diff+1) L[[1]] _
rowMatrix(N)<BR> else L[[N-(Diff+1)]] _
numeric(0)<BR> if(N <= k) L[[N]] _ colMatrix(N)<BR>
}<BR> L[[k]]<BR>}</DIV>
<DIV><BR> </DIV>
<BLOCKQUOTE
style="BORDER-LEFT: #000000 solid 2px; MARGIN-LEFT: 5px; PADDING-LEFT: 5px">
<DIV><FONT face=Arial size=2><B>-----Original Message-----</B><BR><B>From:
</B>Andrew Criswell <<A
href="mailto:arc@arcriswell.com">arc@arcriswell.com</A>><BR><B>To: </B><A
href="mailto:r-help@stat.math.ethz.ch">r-help@stat.math.ethz.ch</A> <<A
href="mailto:r-help@stat.math.ethz.ch">r-help@stat.math.ethz.ch</A>><BR><B>Date:
</B>Tuesday, November 20, 2001 12:52 AM<BR><B>Subject: </B>[R] Sampling from
a population<BR><BR></DIV></FONT>
<DIV><FONT face=Arial size=2>Hi ALL:</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>Suppose you have a population of N <- 5
observations, x <- c(43, 28, 7, 61, 39). From that you can draw a maximum
of 10 samples without replacement of size n <- 3. (Command choose(N,n)
yields 10). For instance the samples I seek are</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> 43, 61, 7</FONT></DIV>
<DIV><FONT face=Arial size=2> 39, 7, 28
...etc</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>How can I get R to do that for me, to get an
exhaustive list of samples of size n drawn without replacement from a
population of size N? The command, sample(x, 3, replace=FALSE), works
well for one draw. Is there a package that will handle multiple
draws?</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>Thanks and best wishes,</FONT></DIV>
<DIV><FONT face=Arial size=2>ANDREW</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>
</FONT></DIV></BLOCKQUOTE></BODY></HTML>