I need to construct all possible combinations of an vector of length N taken X at a time for simulation purposes. Taking a a small vector as an example:>input <- c('a','b','c','d') >somefunction(input)a,b,c a,b,d a,c,d b,c,d my only solution thus far is: somefunction <- function(x){ ...a series of grotesque and horribly inefficient loops deleted to save face. } For my purposes, N=20 and X=5, meaning there are about 15k combinations. I assumed this might be a common problem, but I am coming up empty on my searches of the usual suspect web sites. Thanks in advance for any pointers anyone may have. Michaell -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hello Michaell I've just written this, but it's a bit cranky. I expect some guru will improve my clunky eval(parse()) construction [and, come to think of it, the equally clunky sapply() construction]. all the best robin R> combinations(1:6,3) should work, if combinations <- function(vector,n) { bit <- c(0,1) string <- "binary <- expand.grid(bit" for(i in vector[-1]) { string <- paste(string,",bit") } string <- paste(string,")") eval(parse(text=string)) wanted <- apply(binary,1,sum)==n binary <- binary[wanted,]>0 f <- function(n){vector[binary[n,]]} return(t(sapply(1:dim(binary)[1],f))) }> > > I need to construct all possible combinations of an vector of length N taken > X at a time for simulation purposes. Taking a a small vector as an example: > > > >input <- c('a','b','c','d') > >somefunction(input) > a,b,c > a,b,d > a,c,d > b,c,d > > my only solution thus far is: > > somefunction <- function(x){ > ...a series of grotesque and horribly inefficient loops > deleted to save face. > } > > For my purposes, N=20 and X=5, meaning there are about 15k combinations. > > I assumed this might be a common problem, but I am coming up empty on my searches > of the usual suspect web sites. > > Thanks in advance for any pointers anyone may have. > > Michaell-- Robin Hankin, Lecturer, School of Geography and Environmental Science Tamaki Campus Private Bag 92019 Auckland New Zealand r.hankin at auckland.ac.nz tel 0064-9-373-7599 x6820; FAX 0064-9-373-7042 as of: Fri Nov 8 09:53:00 NZDT 2002 This (linux) system up continuously for: 435 days, 15 hours, 35 minutes -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
The "gregmisc" package provides the 'combinations' function, which can be used as:> combinations(n=4, r=3, c('a','b','c','d') )[,1] [,2] [,3] [1,] "a" "b" "c" [2,] "a" "b" "d" [3,] "a" "c" "d" [4,] "b" "c" "d" where each row contains one possible combination. The 'combinat' package provides the 'combn' function which can be used as:> combn( c('a','b','c','d'), 3 )[,1] [,2] [,3] [,4] [1,] "a" "a" "a" "b" [2,] "b" "b" "c" "c" [3,] "c" "d" "d" "d" where each column contains one possible combination. -Greg> -----Original Message----- > From: Michaell Taylor [mailto:pols1oh at bestweb.net] > Sent: Thursday, November 07, 2002 2:52 PM > To: r-help at stat.math.ethz.ch > Subject: [R] combinations > > > > I need to construct all possible combinations of an vector of > length N taken > X at a time for simulation purposes. Taking a a small vector > as an example: > > > >input <- c('a','b','c','d') > >somefunction(input) > a,b,c > a,b,d > a,c,d > b,c,d > > my only solution thus far is: > > somefunction <- function(x){ > ...a series of grotesque and horribly inefficient loops > deleted to save face. > } > > For my purposes, N=20 and X=5, meaning there are about 15k > combinations. > > I assumed this might be a common problem, but I am coming up > empty on my searches > of the usual suspect web sites. > > Thanks in advance for any pointers anyone may have. > > Michaell > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. > -.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. > _._._._._._._._._ >LEGAL NOTICE Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._