[R] pasting together 2 character arrays

David Winsemius dwinsemius at comcast.net
Tue Aug 31 15:45:37 CEST 2010


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



More information about the R-help mailing list