[R] Export the result k-means cluster to CSV file
S Ellison
S.Ellison at LGCGroup.com
Tue Mar 29 19:04:05 CEST 2016
> 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
More information about the R-help
mailing list