# [R] pairwise combinatons of variables

Christos Hatzis christos at silicoinsights.com
Sat Mar 25 20:26:25 CET 2006

```You can try 'outer'.
Here is a way to get the index combinations that you want in a character
vector:

as.vector(outer(1:4, 1:4, paste, sep=","))

-Christos

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Mark Hempelmann
Sent: Saturday, March 25, 2006 12:50 PM
To: r-help at stat.math.ethz.ch
Subject: [R] pairwise combinatons of variables

Dear WizaRds,

although this might be a trivial question to the community, I was
unable to find anything solving my problem in the help files on CRAN. Please
help.

Suppose I have 4 variables and want to use all possible combinations:
1,2
1,3
1,4
2,3
2,4
3,4
for a further kmeans partitioning.

I tried permutations() of package e1071, but this is not what I need. Thank
you for your help and support.

mark.
---------------------------------------------------------------------

Additionally: For anybody who is willing to offer some advise, here is my
complete approach:
library(e1071)

mat <- matrix( c(6,7,8,2,3,4,12,14,14, 14,15,13,3,1,2,3,4,2,
15,3,10,5,11,7,13,6,1, 15,4,10,6,12,8,12,7,1), ncol=9, byrow=T )
rownames(mat) <- paste("v", 1:4, sep="" )

tmat <- t(mat)
cluster		<- c(1, 2, 1, 3, 3, 3, 1, 2, 2)
centroids	<- matrix( 0, ncol=3, nrow=4 )
obj		<- vector(mode="list", length=3)

for (j in 1:4){
for (i in 1:3){
where <- which(cluster==i) # which obj is in which class?
centroids[j,i] <- mean( tmat[ where,j ] )
obj[[i]] <- tmat[where,]
}
}
colnames(centroids) <- paste( c("Cluster"), 1:3)
rownames(centroids) <- rownames(mat)

centroids
obj

##	now I want to do kmeans of all possible variable pairs, e.g. v1 and
v3
##	automization in a second step later
wjk	<- kmeans(tmat[,c(1,3)], centers=centroids[c(1,3),], iter.max=10,
algorithm="MacQueen")	## obviously wrong

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help