[R] kmeans clustering java

jcg3441 jcg3441 at rit.edu
Tue Apr 5 07:20:41 CEST 2011


Hello, 

I have been trying for a few days to do kmeans on a matrix of data which I
populate from java. 

Here's my code: 

String[] Rargs = {"--vanilla"};
	    Rengine re = new Rengine(Rargs, false, null);
	    System.out.println("Rengine created, waiting for R");
	    // the engine creates R is a new thread, so we should wait until it's
	    // ready
	    if (!re.waitForR()) {
	      System.out.println("Cannot load R");
	      return null;
	    }	
		re.eval ("rmatrix <- matrix(data = NA, nrow = "+rows+", ncol
="+(columns-1)+", byrow = FALSE)");//,dimnames = )");
		REXP rp= re.eval(hDr);
		//loop through the matrix and give the upgma_matrix the correct values
		for (int i = 0; i < rows-1; i++)
		{
		        re.eval ("i<- " +i);
		       for (int j = 1; j < columns; j++)
		        {
		        re.eval ("j<- " +j);
		        //R matrices start at index 1 (java at 0), so add 1 to  current
position
		        re.eval ("ii <- i+1");
		        re.eval ("jj <- j");
		       //add values for the lower triangle..
		        re.eval ("rmatrix [ii,jj] <- "+ data[i][j].toString());
		        System.out.print(data[i][j].toString()+",");
		         }
		       System.out.println();
		 }
	
		REXP rt = re.eval("r_matrix");
		String bindString = "DATAMATRIX <- cbind(rmatrix[,1],";
		for (int k = 0; k<columns-2;k++ ){
			if(k<columns-3){
			bindString = bindString+"rmatrix[,"+(k+2)+"],";
			}else{
				bindString = bindString+"rmatrix[,"+(k+2)+"])";
			}
		}
		
		rt = re.eval(bindString);
			    //cluster using kmeans
		rt = re.eval("DATAMATRIX;");
		rt = re.eval("CLST<-DATAMATRIX");
		rt = re.eval("CLST <- kmeans(CLST,3)",true);

but when I check on rt, it comes back as null. 

Any suggestions? 

Jose

--
View this message in context: http://r.789695.n4.nabble.com/kmeans-clustering-java-tp3427159p3427159.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list