Dear all, there is an R function that return all possible samples of size n, with replacement, from a vector of length N ? Best regards
I am not sure if this is the easiest way, but you can do something like this: library(gregmisc) combinations(N, n, x, repeats=TRUE) where x is an atomic vector of size N. The only restriction is that the x vector has to have N unique elements. The combinations function will return a matrix with n columns containing the combinations. The combinations function is defined recursively. Even for moderate values of N and n it will either take a long time or bomb out with the "evaluation is nested too deeply" error. For example, on my modest machine (PIII 1GHz with 1/5 Gbyte of memory) it takes almost a minute to evaluate combinations(30, 6, repeats=TRUE) (1623160 combinations) and combinations(44, 2, repeats=TRUE) (only 990 combinations) bombs out. If you need to work with larger numbers you will probably need a sequential code. Hope this helps, Andy __________________________________ Andy Jaworski Engineering Systems Technology Center 3M Center, 518-1-01 St. Paul, MN 55144-1000 ----- E-mail: apjaworski at mmm.com Tel: (651) 733-6092 Fax: (651) 736-3122 |---------+--------------------------------> | | "lamack lamack" | | | <lamac_k at hotmail.com>| | | Sent by: | | | r-help-bounces at stat.m| | | ath.ethz.ch | | | | | | | | | 09/17/2003 09:52 | | | | |---------+--------------------------------> >-----------------------------------------------------------------------------------------------------------------------------| | | | To: R-help at stat.math.ethz.ch | | cc: | | Subject: [R] all possible samples | >-----------------------------------------------------------------------------------------------------------------------------| Dear all, there is an R function that return all possible samples of size n, with replacement, from a vector of length N ? Best regards ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
On 17-Sep-03 lamack lamack wrote:> Dear all, there is an R function that return all possible samples of > size n, > with replacement, from a vector of length N ?Without delving into R to see if such a thing exists already, I can suggest the following (based on an algorithm to be found in Nijenhuis and Wilf, "Combinatorial Algortithms", Academic Press 1978). I once implemented it in octave and it worked well. I'll use their notation rather than yours so that I don't confuse my fingers ... To select all subsets of size k out of n (in "lexicographic order"): The n-set is {1,2,3,...,n}. A selected subset is {a[1],a[2],...,a[k]}. Repeatedly apply the following algorithm for "next subset": (A) If first time (first subset): m<-0; h<-k; go to (D) (B) If not first time: if( m>=n-h ) go to (C); else h<-0; (C) h<-(h+1); m<-a[k+1-h]; (D) for(j in (1:h)) {a[k+j-h]<-(m+j)}; if( a[1]==(n-k+1)) final_exit (all done); else nonfinal_exit (some left); For 3 out of 5 this gives {1,2,3},(1,2,4},{1,2,5},{1,3,4},{1,3,5}, {1,4,5},{2,3,4},{2,3,5},{2,4,5},{3,4,5}. Best wishes, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 167 1972 Date: 17-Sep-03 Time: 20:21:32 ------------------------------ XFMail ------------------------------
Hi, Another possibility is to use the combinations function in the gregmisc package, which has an option repeats.allowed. Sorry for not including the original post, but I had deleted it from my mailbox earlier in the day. HTH, Dennis