If possible I would like to combine two different character arrays in combinations Array1 <- c("height","weight","age","sex") Array2 <- c("trt0","trt1","trt2") I would like to combine these two character vectors to end up with such ... Array3 "height.trt0.trt1" "height.trt0.trt2" "weight.trt0.trt1" "weight.trt0.trt2" "age.trt0.trt1" "age.trt0.trt2" "sex.trt0.trt1" "sex.trt0.trt2" I'm trying combinations of the "combinations" and "paste" functions, but no luck yet! Any suggestions about most efficient approach most welcome. Thanks Jim ==============================Dr. Jim Maas University of East Anglia [[alternative HTML version deleted]]
On Aug 31, 2010, at 9:16 AM, Maas James Dr (MED) wrote:> If possible I would like to combine two different character arrays > in combinations > > Array1 <- c("height","weight","age","sex") > > Array2 <- c("trt0","trt1","trt2") > > I would like to combine these two character vectors to end up with > such ... > > Array3 > > "height.trt0.trt1" > "height.trt0.trt2" > "weight.trt0.trt1" > "weight.trt0.trt2" > "age.trt0.trt1" > "age.trt0.trt2" > "sex.trt0.trt1" > "sex.trt0.trt2" >?expand.grid # and noting that "trt0" is handled differently than Array2[2:3] > paste(expand.grid(Array1, Array2[2:3])[,1], Array2[1], expand.grid(Array1, Array2[2:3])[,2], sep=".") [1] "height.trt0.trt1" "weight.trt0.trt1" "age.trt0.trt1" "sex.trt0.trt1" "height.trt0.trt2" [6] "weight.trt0.trt2" "age.trt0.trt2" "sex.trt0.trt2" -- David Winsemius, MD West Hartford, CT
Jim, It is not completely clear how you want to handle the items in Array2, but perhaps something like the following does what you needs (or at least points you in the right direction): paste( rep(Array1, each=2), rep(Array2[1], times=8), rep(Array2[2:3], times=4), sep='.') Regards, Jan Quoting "Maas James Dr (MED)" <J.Maas at uea.ac.uk>:> If possible I would like to combine two different character arrays > in combinations > > Array1 <- c("height","weight","age","sex") > > Array2 <- c("trt0","trt1","trt2") > > I would like to combine these two character vectors to end up with such ... > > Array3 > > "height.trt0.trt1" > "height.trt0.trt2" > "weight.trt0.trt1" > "weight.trt0.trt2" > "age.trt0.trt1" > "age.trt0.trt2" > "sex.trt0.trt1" > "sex.trt0.trt2" > > I'm trying combinations of the "combinations" and "paste" functions, > but no luck yet! > Any suggestions about most efficient approach most welcome. > Thanks > > Jim > > ==============================> Dr. Jim Maas > University of East Anglia > > > [[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. >
Try this: with(expand.grid(Array1, combn(Array2, 2, paste, collapse = '.')[-3]), paste(Var1, Var2, sep = '.')) On Tue, Aug 31, 2010 at 10:16 AM, Maas James Dr (MED) <J.Maas@uea.ac.uk>wrote:> If possible I would like to combine two different character arrays in > combinations > > Array1 <- c("height","weight","age","sex") > > Array2 <- c("trt0","trt1","trt2") > > I would like to combine these two character vectors to end up with such ... > > Array3 > > "height.trt0.trt1" > "height.trt0.trt2" > "weight.trt0.trt1" > "weight.trt0.trt2" > "age.trt0.trt1" > "age.trt0.trt2" > "sex.trt0.trt1" > "sex.trt0.trt2" > > I'm trying combinations of the "combinations" and "paste" functions, but no > luck yet! > Any suggestions about most efficient approach most welcome. > Thanks > > Jim > > ==============================> Dr. Jim Maas > University of East Anglia > > > [[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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Always good to have that fourth way, just in case ....> (Array3 <- as.vector(outer(Array2[-1], Array1, FUN=function(x, y) paste(y, Array2[1], x, sep="."))))[1] "height.trt0.trt1" "height.trt0.trt2" "weight.trt0.trt1" "weight.trt0.trt2" "age.trt0.trt1" "age.trt0.trt2" [7] "sex.trt0.trt1" "sex.trt0.trt2" HTH, David L. Reiner XR Trading LLC -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Maas James Dr (MED) Sent: Tuesday, August 31, 2010 8:17 AM To: r-help at r-project.org Subject: [R] pasting together 2 character arrays If possible I would like to combine two different character arrays in combinations Array1 <- c("height","weight","age","sex") Array2 <- c("trt0","trt1","trt2") I would like to combine these two character vectors to end up with such ... Array3 "height.trt0.trt1" "height.trt0.trt2" "weight.trt0.trt1" "weight.trt0.trt2" "age.trt0.trt1" "age.trt0.trt2" "sex.trt0.trt1" "sex.trt0.trt2" I'm trying combinations of the "combinations" and "paste" functions, but no luck yet! Any suggestions about most efficient approach most welcome. Thanks Jim ==============================Dr. Jim Maas University of East Anglia [[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. This e-mail and any materials attached hereto, including, without limitation, all content hereof and thereof (collectively, "XR Content") are confidential and proprietary to XR Trading, LLC ("XR") and/or its affiliates, and are protected by intellectual property laws. Without the prior written consent of XR, the XR Content may not (i) be disclosed to any third party or (ii) be reproduced or otherwise used by anyone other than current employees of XR or its affiliates, on behalf of XR or its affiliates. THE XR CONTENT IS PROVIDED AS IS, WITHOUT REPRESENTATIONS OR WARRANTIES OF ANY KIND. TO THE MAXIMUM EXTENT PERMISSIBLE UNDER APPLICABLE LAW, XR HEREBY DISCLAIMS ANY AND ALL WARRANTIES, EXPRESS AND IMPLIED, RELATING TO THE XR CONTENT, AND NEITHER XR NOR ANY OF ITS AFFILIATES SHALL IN ANY EVENT BE LIABLE FOR ANY DAMAGES OF ANY NATURE WHATSOEVER, INCLUDING, BUT NOT LIMITED TO, DIRECT, INDIRECT, CONSEQUENTIAL, SPECIAL AND PUNITIVE DAMAGES, LOSS OF PROFITS AND TRADING LOSSES, RESULTING FROM ANY PERSON'S USE OR RELIANCE UPON, OR INABILITY TO USE, ANY XR CONTENT, EVEN IF XR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGES OR IF SUCH DAMAGES WERE FORESEEABLE.