1
mydata<- read.csv("q.csv")
# K-Means Cluster Analysis
fit <- kmeans(mydata, 3) # 3 cluster solution
# get cluster means
abc<-aggregate(mydata,by=list(fit$cluster),FUN=mean)
abc[1]
# append cluster assignment
mydata <- data.frame(mydata, fit$cluster) 
mydata

how do i access individual values of the k means result? Im able to access only the single vector using abc[1]

  • 2
    If would be helpful if you included sample data in your question to make your problem [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). For the sample data, be clear on exactly what value you are trying to extract. – MrFlick May 10 '16 at 13:21
  • 1
    Try `str(fit)` to take a look at what this object contains. – lmo May 10 '16 at 13:46
  • q.csv contains three columns of data consisting of 200 entries – alvina monteiro May 10 '16 at 14:26

1 Answers1

1

In case nrow(mydata) == length(fit$cluster), I would try to use cbind function. Otherwise you won't be able to do it.

  • can rows be extracted as an array? – alvina monteiro May 10 '16 at 15:18
  • I would say you can extract it natively as a vector. In case you want to create an array, best way would be by casting data like: `array(fit$cluster)`, although there are other structures more friendly and efficient, such as data.frame, matrix... that I strongly recommend. – Francisco Calvo Pérez May 11 '16 at 07:09