Skip to content
Back to formatted view

Raw Message

Message-ID: <CAAmySGMpo-iXZrEL-nn-oQJh9xXLP1oaPm8AJ-N1+qfdhoW3eA@mail.gmail.com>
Date: 2011-12-06T12:58:27Z
From: R. Michael Weylandt
Subject: Paste() - Get all possible combinations from multiple vectors
In-Reply-To: <2D1C3C74C8DE6C48B602E679AB37211C0AA947527E@UM-MAIL4112.unimaas.nl>

outer(outer(x, y, paste), z, paste)

Michael

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
> 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.