Hello Everyone :?i have ?done the clustering ?process by k-means cluster, then i try to save[Export ] the groups of clustering, to txt, or CSV files , how i can do that #########################################clusrer.data <- function(data,n) { miRNA.exp.cluster <- scale(t(miRNA.exp)) k.means.fit <- kmeans(miRNA.exp.cluster,n)k.means.fit#i try to save the results of k-means cluster by this code :? k.means.fit <- as.data.frame(k.means.fit)write.csv(k.means.fit, file="k-meanReslut.csv") #x<-k.means.fit$clusters#write.csv(x, file="k-meanReslut.csv") } the result:K-means clustering with 5 clusters of sizes 8, 6, 7, 20, 18 ####################33 thanks for all. ?? [[alternative HTML version deleted]]
> Hello Everyone :?i have ?done the clustering ?process by k-means cluster, then i > try to save[Export ] the groups of clustering, to txt, or CSV files , how i can do > thatDepends what you want. You can save the whole object for reloading using save() The cluster members can be written to text files using write(cl$cluster, file="<some file name>"). The whole thing can be written, without names, using something like lapply(cl, write, file="cluster.txt", append=TRUE) #Using cl from the first example in ?kmeans If you need sensible breaks and names in the file, you'll have to roll your own alternative write routines. For example Write <- function(NN, x, filename="cluster.txt", ...) { write(NN, filename, append=TRUE, ...) write(x[[NN]], filename, append=TRUE, ...) cat("\n", file=filename, append=TRUE, ...) } lapply(names(cl), Write, x=cl, filename="myclust.txt") (Ignore the NULLs returned) Anything more structured than that and you'll have to write a write.kmeans replacement for write that structures the results as you need them later. S Ellison ******************************************************************* This email and any attachments are confidential. Any use, copying or disclosure other than by the intended recipient is unauthorised. If you have received this message in error, please notify the sender immediately via +44(0)20 8943 7000 or notify postmaster at lgcgroup.com and delete this message and any copies from your computer and network. LGC Limited. Registered in England 2991879. Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK
> i am confusing about your code , i can not get the the desired result . can you > more clear for the code?The code I suggested just provided a structured text output of the kmeans object. If you want a cluster-by-cluster output of the original data, you will need to write a function that does what you want for each cluster. For example you could write a function that would take a cluster index as the first argument, the kmeans object as a second argument, and the original data as the third. Then you can use something like lapply to apply that function to each subset of your data. For example, again using the example in kmeans, a simple list of the cluster coordinate sets can be obtained using by.index <- function(index, km, x) { x[km$cluster==index, ] #returns the subset of original data in cluster (index) } lapply(unique(cl$cluster), by.index, km=cl, x=x) #cl was the kmeans object generated from the data set x used in the example #Using unique(cl$cluster) means that all the clusters identified in the kmeans object are included in the list This generates a list, identified by cluster index number, that contains the coordinates for each cluster. If you assign that list to an object you can then write a separate function to format and write out the coordinates and use lapply to run that on the list. Or you can include formatted write calls in the by.index function, writing to a file as before. S Ellison ******************************************************************* This email and any attachments are confidential. Any use, copying or disclosure other than by the intended recipient is unauthorised. If you have received this message in error, please notify the sender immediately via +44(0)20 8943 7000 or notify postmaster at lgcgroup.com and delete this message and any copies from your computer and network. LGC Limited. Registered in England 2991879. Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK