Campbell, Desmond
2011-Aug-27 16:54 UTC
[R] all combinations of the elements of two vectors
Dear R-help readers, I'm sure this problem has been answered but I can't find the solution. I have two vectors v1 <- c("a","b") v2 <- c(1,2,3) I want an easy way to produce every possible combination of v1, v2 elements Ie I want to produce c("a1","a2","a3", "b1","b2","b3") regards Desmond Desmond Campbell Dept of Biostatistics and Computing, Institute of Psychiatry (KCL), PO Box 20, De Crespigny Park, Denmark Hill London, SE5 8AF Tel 020 7848 0309 Email D.Campbell@iop.kcl.ac.uk<mailto:D.Campbell@iop.kcl.ac.uk> [[alternative HTML version deleted]]
Hi Desmond, You might try> sort(apply(expand.grid(v1, v2), 1, paste, collapse = "", sep = ""))[1] "a1" "a2" "a3" "b1" "b2" "b3" HTH, Jorge On Sat, Aug 27, 2011 at 12:54 PM, Campbell, Desmond <> wrote:> Dear R-help readers, > > I'm sure this problem has been answered but I can't find the solution. > > I have two vectors > v1 <- c("a","b") > v2 <- c(1,2,3) > I want an easy way to produce every possible combination of v1, v2 elements > Ie I want to produce > c("a1","a2","a3", "b1","b2","b3") > > regards > Desmond > > Desmond Campbell > Dept of Biostatistics and Computing, Institute of Psychiatry (KCL), > PO Box 20, De Crespigny Park, Denmark Hill London, SE5 8AF > > Tel 020 7848 0309 > Email D.Campbell@iop.kcl.ac.uk<mailto:D.Campbell@iop.kcl.ac.uk> > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
x<-letters[1:3] y<-1:3 d<-expand.grid(x,y) g<-apply(d,1,function(x) paste(x[1],x[2],sep="")) HTH, Daniel Campbell, Desmond-2 wrote:> > Dear R-help readers, > > I'm sure this problem has been answered but I can't find the solution. > > I have two vectors > v1 <- c("a","b") > v2 <- c(1,2,3) > I want an easy way to produce every possible combination of v1, v2 > elements > Ie I want to produce > c("a1","a2","a3", "b1","b2","b3") > > regards > Desmond > > Desmond Campbell > Dept of Biostatistics and Computing, Institute of Psychiatry (KCL), > PO Box 20, De Crespigny Park, Denmark Hill London, SE5 8AF > > Tel 020 7848 0309 > Email D.Campbell at iop.kcl.ac.uk<mailto:D.Campbell at iop.kcl.ac.uk> > > > > [[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. >-- View this message in context: http://r.789695.n4.nabble.com/all-combinations-of-the-elements-of-two-vectors-tp3773397p3773472.html Sent from the R help mailing list archive at Nabble.com.
Hi> > Dear R-help readers, > > I'm sure this problem has been answered but I can't find the solution. > > I have two vectors > v1 <- c("a","b") > v2 <- c(1,2,3) > I want an easy way to produce every possible combination of v1, v2elements> Ie I want to produce > c("a1","a2","a3", "b1","b2","b3")Another option is z<-outer(x,y, paste, sep="") dim(z)<-NULL> z[1] "a1" "b1" "c1" "a2" "b2" "c2" "a3" "b3" "c3" which gives the result in different order or z<-as.vector(t(z))> z[1] "a1" "a2" "a3" "b1" "b2" "b3" "c1" "c2" "c3" Which gives you desired order. Regards Petr> > regards > Desmond > > Desmond Campbell > Dept of Biostatistics and Computing, Institute of Psychiatry (KCL), > PO Box 20, De Crespigny Park, Denmark Hill London, SE5 8AF > > Tel 020 7848 0309 > Email D.Campbell at iop.kcl.ac.uk<mailto:D.Campbell at iop.kcl.ac.uk> > > > > [[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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.