[R] Paste() - Get all possible combinations from multiple vectors
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@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.
Re: [R] Paste() - Get all possible combinations from multiple vectors
outer(outer(x, y, paste), z, paste) Michael On Tue, Dec 6, 2011 at 7:53 AM, Gaj Stan (BIGCAT) stan@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@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. __ 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.
Re: [R] Paste() - Get all possible combinations from multiple vectors
Hi. Maybe this will help you: expand.grid(x,y,z) apply(expand.grid(x,y,z),1, function (x) paste(x[1], x[2], x[3], sep=)) Andrija On Tue, Dec 6, 2011 at 1:53 PM, Gaj Stan (BIGCAT) stan@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@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]] __ 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.