# Re: [R] looping using combinatorics

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Fri 14 Jul 2006 - 12:39:08 EST

I assume your question is given 3 vectors of the same length: a, b and c how do we loop over pairs of them. In the following each iteration displays one pair:

library(combinat)
DF <- data.frame(a = 1:4, b = 5:8, c = 9:12)    for(idx in as.data.frame(combn(3,2))) print(DF[,idx])

On 7/13/06, Jesse Albert Canchola <jesse.canchola.b@bayer.com> wrote:
> I have a problem where I need to loop over the total combinations of
> vectors (combined once chosen via combinatorics). Here is a
> simplification of the problem:
>
> STEP 1: Define three vectors a, b, c.
> STEP 2: Combine all possible pairwise vectors (i.e., 3 choose 2 = 3
> possible pairs of vectors: ab,ac, bc)
> NOTE: the actual problem has 8 choose 4, 8 choose 5 and 8 choose 6
> combinations.
> STEP 3: Do the same math on each pairwise combination and spit out
>
> ####### BEGIN CODE #######
> #STEP 1
> a1 <- c(1,2,3,4,5,6,7,8,9,10,11,12)
> a <- matrix(a1,2,3,byrow=T)
> a
>
> b1 <- c(13,14,15,16,17,18,19,20,21,22,23,24)
> b <- matrix(b1,2,3,byrow=T)
> b
>
> c1 <- c(25,26,27,28,29,30,31,32,33,34,35,36)
> c <- matrix(b1,2,3,byrow=T)
> c
>
> # example: combine the first two vectors "a" and "b"
> combab <- rbind(a,b)
>
> # the a,b combined data from the algorithm later below should look like
> # something like the following:
> combab
>
> # use the combinatorics "combn" function found in the "combinat" package
> on CRAN
> m <- combn(3,2) # three choose two combinations
> m
>
> # the first assignment below should be numeric and then subsequent
> # assignments as character since the first time you assign a number to
> # a character in a matrix the rest of the numbers in the matrix are
> coerced to character
> m[m==1]='a'; m[m=='2']='b'; m[m=='3']='c'
> m
>
> #STEP 2: combine pairwise vectors into a matrix or frame
> for (i in dim(m)[1])
> for (j in dim(m)[2])
> {
> combined <-
> rbind(cat(format(m[i]),"\n"),cat(format(m[j]),"\n")) #cat/format removes
> the quotes
> combined
> }
> traceback()
>
>
> #STEP 3: {not there yet}
> ################# END CODE ################
>
> The problem is that in STEP 2 (not complete), the results in the rbind are
> not recognized as the objects they represent (i.e., the "a" without quotes
> is not recognized as the data object we defined in STEP 1. Perhaps this
> is a parsing problem. Perhaps there is an alterative way to do this. I
> looked pretty long and hard in the CRAN libraries but alas, I am stuck.

> BTW, I picked up R about a month ago (I used primarily SAS, Stata and
> SPSS).
>
> Regards and TIA,
> Jesse
>
>
>
>
>
>
> Jesse A. Canchola
> Biostatistician III
> Bayer Healthcare
> 725 Potter St.
> Berkeley, CA 94710
> P: 510.705.5855
> F: 510.705.5718
> E: Jesse.Canchola.b@Bayer.Com
>
>
>
>
> _______________________________________________________________________________________________
>
> The information contained in this e-mail is for the exclusive use of the intended recipient(s) and may be confidential, proprietary, and/or legally privileged. Inadvertent disclosure of this message does not constitute a waiver of any privilege. If you receive this message in error, please do not directly or indirectly use, print, copy, forward, or disclose any part of this message. Please also delete this e-mail and all copies and notify the sender. Thank you.

>
> For alternate languages please go to http://bayerdisclaimer.bayerweb.com
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help