<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 5.50.4134.600" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#d8d0c8>
<DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<BLOCKQUOTE dir=ltr
style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<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><FONT face="Courier New" size=2></FONT> </DIV>
<DIV> </DIV>
<DIV><FONT face="Courier New" size=2># First solution</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face="Courier New" size=2>combix<-function(x,r)
{<BR>n<-length(x)<BR>total<-choose(n,r)<BR>a<-matrix(0,total,r)<BR>ind<-c(1:r)<BR>for
(i in 1:total){<BR> for (j in
1:r){a[i,j]<-x[ind[j]]}<BR> for (j in
r:1){<BR>
miro<-j<BR>
ind[j]<-ind[j]+1<BR> if (ind[j]<n-r+j+1)
break()<BR>}<BR> while(miro<r) {<BR>
ind[miro+1]<-ind[miro]+1<BR>
miro<-miro+1<BR>
}<BR>}<BR>return(a)<BR>}</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face="Courier New" size=2>print(combix(x=1:5,r=3))</FONT></DIV>
<DIV> </DIV><FONT face="Courier New" size=2>
<DIV><BR>x<-c(8,3,1,11,4,7)<BR>print(mean(x))</DIV>
<DIV> </DIV>
<DIV><BR>medias<- apply(combix(x,2),1,mean)<BR>print(mean(medias))</DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV>#With-Replacement</DIV>
<DIV> </DIV>
<DIV>combixr<-function(x,r)
{<BR>n<-length(x)<BR>total<-n**r<BR>a<-matrix(0,total,r)<BR>ind<-rep(1,r)<BR>for
(i in 1:total){<BR> for (j in
1:r){a[i,j]<-x[ind[j]]}<BR> for (j in
r:1){<BR>
miro<-j<BR>
ind[j]<-ind[j]+1<BR> if (ind[j]<n+1)
break()<BR>}<BR> while(miro<r) {<BR>
ind[miro+1]<-1<BR>
miro<-miro+1<BR>
}<BR>}<BR>return(a)<BR>}<BR>print(combixr(x=1:5,r=3))</DIV>
<DIV> </DIV>
<DIV><BR>x<-c(8,3,1,11,4,7)<BR>print(mean(x))</DIV>
<DIV> </DIV>
<DIV><BR>medias<- apply(combixr(x,2),1,mean)<BR>mean(medias)</DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV>#Other form</DIV>
<DIV> </DIV>
<DIV><BR>darcombi<-
function(N,n,caso){<BR>vecpeq<-rep(0,n)<BR>ante<-0<BR>ante1<-0<BR>for(i
in 1:n){<BR>N1<- N-(ante1+1)<BR>n1<-
n-i<BR>v1<-cumsum(choose(N1:n1,n1))<BR>ante<-min(which(caso<=v1))<BR>vecpeq[i]<-ante+ante1<BR>resta<-0<BR>if
(ante>1)
resta<-v1[ante-1]<BR>caso<-caso-resta<BR>ante1<-vecpeq[i]<BR>}<BR>return(vecpeq)<BR>}</DIV>
<DIV> </DIV>
<DIV>N<-5<BR>n<-3</DIV>
<DIV> </DIV>
<DIV>for (i in 1:choose(N,n)){<BR>print(darcombi(N=N,n=n,i))<BR>}</DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV>x <- c(43, 28, 7, 61, 39)<BR>N<-length(x)<BR>n<-3</DIV>
<DIV> </DIV>
<DIV>for (i in
1:choose(N,n)){<BR>print(x[darcombi(N=N,n=n,i)])<BR>}<BR></FONT><FONT
face=Arial size=2>
</FONT></DIV></BLOCKQUOTE></DIV></BODY></HTML>