# [R] looping using combinatorics

From: Jesse Albert Canchola <jesse.canchola.b_at_bayer.com>
Date: Fri 14 Jul 2006 - 08:40:46 EST

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 answers each time

####### 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)

# 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

```P: 510.705.5855
F: 510.705.5718
E: Jesse.Canchola.b@Bayer.Com

_______________________________________________________________________________________________

```

For alternate languages please go to http://bayerdisclaimer.bayerweb.com

R-help@stat.math.ethz.ch mailing list