[R] Paste() - Get all possible combinations from multiple vectors
R. Michael Weylandt
michael.weylandt at gmail.com
Tue Dec 6 13:58:27 CET 2011
outer(outer(x, y, paste), z, paste)
On Tue, Dec 6, 2011 at 7:53 AM, Gaj Stan (BIGCAT)
<stan.gaj at maastrichtuniversity.nl> wrote:
> Hello fellow R-users,
> Given are three vectors and the outcome would be all possible combinations in combination with the paste() function.
> For example:
> x <- c(1:3)
> y <- letters[1:3]
> z <- LETTERS[1:3]
> My result would thus be 18 names based on all possible combinations between these vectors:
> "1 a A" "1 a B", "1 a C", "1 b A", "1 b B", "1 b C", "1 c A", "1 c B", "1 c C", etc.
> To solve the issue above I would use:
> paste(rep(x, each=9), rep(y, each=6), z)
> This is a very straightforward example, but when I have vectors of different sizes, it will be much more difficult (for me) to create this. Is there an easier way to do this?
> Many thanks in advance,
> -- Stan
> [[alternative HTML version deleted]]
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help