Hi all, I'm trying to combine exhaustively several character arrays in R like: x=c("one","two","three") y=c("yellow","blue","green") z=c("apple","cheese") in order to get concatenation of x[1] y[1] z[1] ("one yellow apple") x[1] y[1] z[2] ("one yellow cheese") x[1] y[2] z[1]("one blue apple") ... x[length(x)] y[length(y)] z[length(z)] ("three green cheese") Anyone has a solution ? Thank in advance -- View this message in context: http://r.789695.n4.nabble.com/Combining-characters-tp4261888p4261888.html Sent from the R help mailing list archive at Nabble.com.
Hello, If you want to apply the same procedure to all elements of an object, check out the '*apply' functions. In this case, x=c("one","two","three") y=c("yellow","blue","green") z=c("apple","cheese") lapply(x, function(x) paste(x, y)) gives a good picture of what you want to do, just transform it into a function and use the function: f <- function(x, y) unlist(lapply(x, function(x) paste(x,y))) f(x, f(y, z)) With more than 3 vectors, you could try a recursive version. I hope this helps. Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/Combining-characters-tp4261888p4262632.html Sent from the R help mailing list archive at Nabble.com.
? expand.grid Michael On Wed, Jan 4, 2012 at 10:32 AM, jeremy <jeremynamerica at gmail.com> wrote:> Hi all, > > I'm trying to combine exhaustively several character arrays in R like: > x=c("one","two","three") > y=c("yellow","blue","green") > z=c("apple","cheese") > > in order to get concatenation of > > x[1] y[1] z[1] ?("one yellow apple") > x[1] y[1] z[2] ("one yellow cheese") > x[1] y[2] z[1]("one blue apple") > ... > x[length(x)] y[length(y)] z[length(z)] ?("three green cheese") > > Anyone has a solution ? > Thank in advance > > -- > View this message in context: http://r.789695.n4.nabble.com/Combining-characters-tp4261888p4261888.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. You can use expand.grid here expand.grid(x,y,z) Andrija On Wed, Jan 4, 2012 at 5:32 PM, jeremy <jeremynamerica at gmail.com> wrote:> Hi all, > > I'm trying to combine exhaustively several character arrays in R like: > x=c("one","two","three") > y=c("yellow","blue","green") > z=c("apple","cheese") > > in order to get concatenation of > > x[1] y[1] z[1] ?("one yellow apple") > x[1] y[1] z[2] ("one yellow cheese") > x[1] y[2] z[1]("one blue apple") > ... > x[length(x)] y[length(y)] z[length(z)] ?("three green cheese") > > Anyone has a solution ? > Thank in advance > > -- > View this message in context: http://r.789695.n4.nabble.com/Combining-characters-tp4261888p4261888.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 Jan 4, 2012, at 10:32 AM, jeremy wrote:> Hi all, > > I'm trying to combine exhaustively several character arrays in R like: > x=c("one","two","three") > y=c("yellow","blue","green") > z=c("apple","cheese") > > in order to get concatenation of > > x[1] y[1] z[1] ("one yellow apple") > x[1] y[1] z[2] ("one yellow cheese") > x[1] y[2] z[1]("one blue apple") > ... > x[length(x)] y[length(y)] z[length(z)] ("three green cheese") > > Anyone has a solution ? > Thank in advanceSee ?expand.grid and ?paste DF <- expand.grid(x, y, z)> DFVar1 Var2 Var3 1 one yellow apple 2 two yellow apple 3 three yellow apple 4 one blue apple 5 two blue apple 6 three blue apple 7 one green apple 8 two green apple 9 three green apple 10 one yellow cheese 11 two yellow cheese 12 three yellow cheese 13 one blue cheese 14 two blue cheese 15 three blue cheese 16 one green cheese 17 two green cheese 18 three green cheese> with(DF, paste(Var1, Var2, Var3))[1] "one yellow apple" "two yellow apple" "three yellow apple" [4] "one blue apple" "two blue apple" "three blue apple" [7] "one green apple" "two green apple" "three green apple" [10] "one yellow cheese" "two yellow cheese" "three yellow cheese" [13] "one blue cheese" "two blue cheese" "three blue cheese" [16] "one green cheese" "two green cheese" "three green cheese" HTH, Marc Schwartz
apply(expand.grid(x, y, z, stringsAsFactors=F), 1, paste, collapse=' ') On Wed, Jan 4, 2012 at 8:32 AM, jeremy <jeremynamerica@gmail.com> wrote:> Hi all, > > I'm trying to combine exhaustively several character arrays in R like: > x=c("one","two","three") > y=c("yellow","blue","green") > z=c("apple","cheese") > > in order to get concatenation of > > x[1] y[1] z[1] ("one yellow apple") > x[1] y[1] z[2] ("one yellow cheese") > x[1] y[2] z[1]("one blue apple") > ... > x[length(x)] y[length(y)] z[length(z)] ("three green cheese") > > Anyone has a solution ? > Thank in advance > > -- > View this message in context: > http://r.789695.n4.nabble.com/Combining-characters-tp4261888p4261888.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]]
Try expand.grid() to create all the combinations. Then just collapse them with paste(): apply(expand.grid(x, y, z), 1, paste, collapse = " ") Cheers, Josh On Wed, Jan 4, 2012 at 8:32 AM, jeremy <jeremynamerica at gmail.com> wrote:> Hi all, > > I'm trying to combine exhaustively several character arrays in R like: > x=c("one","two","three") > y=c("yellow","blue","green") > z=c("apple","cheese") > > in order to get concatenation of > > x[1] y[1] z[1] ?("one yellow apple") > x[1] y[1] z[2] ("one yellow cheese") > x[1] y[2] z[1]("one blue apple") > ... > x[length(x)] y[length(y)] z[length(z)] ?("three green cheese") > > Anyone has a solution ? > Thank in advance > > -- > View this message in context: http://r.789695.n4.nabble.com/Combining-characters-tp4261888p4261888.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.-- Joshua Wiley Ph.D. Student, Health Psychology Programmer Analyst II, Statistical Consulting Group University of California, Los Angeles https://joshuawiley.com/