[R] Combining characters

Joshua Wiley jwiley.psych at gmail.com
Wed Jan 4 22:52:29 CET 2012


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/



More information about the R-help mailing list