Dear all, I want to concatenate the elements of two vectors such as a<-c("a1","a2") b<-c("b1","b2") and obtain "a1b1", "a1b2","a2b1","a2b2" I tried the paste and paste0 functions, but they yielded elementwise concatenation such as "a1b1","a2b2" I am wondering that is there an efficient way of doing my wish, for instance without using for loop etc. Best Ozgur -- View this message in context: http://r.789695.n4.nabble.com/concatenating-two-vectors-tp4643139.html Sent from the R help mailing list archive at Nabble.com.
Dear Ozgur, Try sort(apply(expand.grid(a, b), 1, paste0, collapse = "")) HTH, Jorge.- On Fri, Sep 14, 2012 at 8:54 AM, Özgür Asar <> wrote:> Dear all, > > I want to concatenate the elements of two vectors such as > > a<-c("a1","a2") > b<-c("b1","b2") > > and obtain > > "a1b1", "a1b2","a2b1","a2b2" > > I tried the paste and paste0 functions, but they yielded elementwise > concatenation such as > > "a1b1","a2b2" > > I am wondering that is there an efficient way of doing my wish, for > instance > without using for loop etc. > > Best > > Ozgur > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/concatenating-two-vectors-tp4643139.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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]]
On Fri, Sep 14, 2012 at 1:54 PM, ?zg?r Asar <oasar at metu.edu.tr> wrote:> Dear all, > > I want to concatenate the elements of two vectors such as > > a<-c("a1","a2") > b<-c("b1","b2") > > and obtain > > "a1b1", "a1b2","a2b1","a2b2" > > I tried the paste and paste0 functions, but they yielded elementwise > concatenation such as > > "a1b1","a2b2" > > I am wondering that is there an efficient way of doing my wish, for instance > without using for loop etc.This is in the general frame of things which look like 'outer' (or Cartesian) products so you'll want to use the outer() function. I believe outer(a, b, FUN = paste0) should work. Cheers, Michael> > Best > > Ozgur > > > > -- > View this message in context: http://r.789695.n4.nabble.com/concatenating-two-vectors-tp4643139.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
Hi, Try this: ?paste0(expand.grid(a,b)$Var1,expand.grid(a,b)$Var2) [1] "a1b1" "a2b1" "a1b2" "a2b2" A.K. ----- Original Message ----- From: ?zg?r Asar <oasar at metu.edu.tr> To: r-help at r-project.org Cc: Sent: Friday, September 14, 2012 8:54 AM Subject: [R] concatenating two vectors Dear all, I want to concatenate the elements of two vectors such as a<-c("a1","a2") b<-c("b1","b2") and obtain "a1b1", "a1b2","a2b1","a2b2" I tried the paste and paste0 functions, but they yielded elementwise concatenation such as "a1b1","a2b2" I am wondering that is there an efficient way of doing my wish, for instance without using for loop etc. Best Ozgur -- View this message in context: http://r.789695.n4.nabble.com/concatenating-two-vectors-tp4643139.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.
On 14-09-2012, at 14:54, ?zg?r Asar wrote:> Dear all, > > I want to concatenate the elements of two vectors such as > > a<-c("a1","a2") > b<-c("b1","b2") > > and obtain > > "a1b1", "a1b2","a2b1","a2b2" > > I tried the paste and paste0 functions, but they yielded elementwise > concatenation such asThis is also an option sort(outer(a,b,paste0)) Berend