Dear All, I wonder if there is any R function to generate a sequence of vectors from existing ones. For example: x 1<- c(1,2,3) x2 <- c(4,5) x3 <- c(6,7,8) The desired output is a list of all 3*2*3 = 18 possible combinations of elements of x1,x2 and x3. One element for example is (1,4,6). Many thanks in advance, Bernard --------------------------------- [[alternative HTML version deleted]]
?expand.grid should do what you want. --- Marc Bernard <bernarduse1 at yahoo.fr> wrote:> Dear All, > > I wonder if there is any R function to generate a > sequence of vectors from existing ones. For example: > x 1<- c(1,2,3) > x2 <- c(4,5) > x3 <- c(6,7,8) > > The desired output is a list of all 3*2*3 = 18 > possible combinations of elements of x1,x2 and x3. > One element for example is (1,4,6). > > Many thanks in advance, > > Bernard > > > > > --------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, > reproducible code. >
expand.grid() is what you are looking for. expand.grid(x1, x2, x3) HTH, Thierry ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx op inbo.be www.inbo.be Do not put your faith in what statistics say until you have carefully considered what they do not say. ~William W. Watt A statistical analysis, properly conducted, is a delicate dissection of uncertainties, a surgery of suppositions. ~M.J.Moroney -----Oorspronkelijk bericht----- Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org] Namens Marc Bernard Verzonden: maandag 19 november 2007 16:00 Aan: r-help op stat.math.ethz.ch Onderwerp: [R] sequence of vectors Dear All, I wonder if there is any R function to generate a sequence of vectors from existing ones. For example: x 1<- c(1,2,3) x2 <- c(4,5) x3 <- c(6,7,8) The desired output is a list of all 3*2*3 = 18 possible combinations of elements of x1,x2 and x3. One element for example is (1,4,6). Many thanks in advance, Bernard --------------------------------- [[alternative HTML version deleted]] ______________________________________________ R-help op r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
try: expand.grid(x1, x2, x3) Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Marc Bernard" <bernarduse1 at yahoo.fr> To: <r-help at stat.math.ethz.ch> Sent: Monday, November 19, 2007 3:59 PM Subject: [R] sequence of vectors> Dear All, > > I wonder if there is any R function to generate a sequence of > vectors from existing ones. For example: > x 1<- c(1,2,3) > x2 <- c(4,5) > x3 <- c(6,7,8) > > The desired output is a list of all 3*2*3 = 18 possible > combinations of elements of x1,x2 and x3. One element for example is > (1,4,6). > > Many thanks in advance, > > Bernard > > > > > --------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Well, this is a natural thing to program up using 3 nested 'for', loops. Alternatively, one could use something like: > combn <- function( ..., l=list(...) ) + { + lens <- sapply( args, length) + ncomb <- prod(lens) + retval <- matrix(ncol=length(args), nrow=ncomb) + for(i in 1:length(args)) + { + retval[,i] <- rep(args[[i]], length=ncomb) + } + retval + } > combn(a=1:3, b=4:5, c=6:8) [,1] [,2] [,3] [1,] 1 4 6 [2,] 2 5 7 [3,] 3 4 8 [4,] 1 5 6 [5,] 2 4 7 [6,] 3 5 8 [7,] 1 4 6 [8,] 2 5 7 [9,] 3 4 8 [10,] 1 5 6 [11,] 2 4 7 [12,] 3 5 8 [13,] 1 4 6 [14,] 2 5 7 [15,] 3 4 8 [16,] 1 5 6 [17,] 2 4 7 [18,] 3 5 8 On Nov 19, 2007, at 9:59AM , Marc Bernard wrote:> Dear All, > > I wonder if there is any R function to generate a sequence of > vectors from existing ones. For example: > x 1<- c(1,2,3) > x2 <- c(4,5) > x3 <- c(6,7,8) > > The desired output is a list of all 3*2*3 = 18 possible > combinations of elements of x1,x2 and x3. One element for example > is (1,4,6). > > Many thanks in advance, > > Bernard > > > > > --------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.