[R] pairwise combinatons of variables

Mark Hempelmann neo27 at t-online.de
Sat Mar 25 18:50:01 CET 2006


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




More information about the R-help mailing list