[R] kmeans clustering java

From: jcg3441 <jcg3441_at_rit.edu>
Date: Tue, 05 Apr 2011 00:20:41 -0500 (CDT)


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?


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.

R-help_at_r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Tue 05 Apr 2011 - 08:13:09 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 05 Apr 2011 - 08:20:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive