0

When I run my kmeans algorithm I get this error

Error in kmeans(dat[, -1], k) : 
more cluster centers than distinct data points.

How can this error be fixed? I am 99% sure my data points are distinct. Here is my code

withinVar <- c() 
for (k in 2:30) { 
  km <- kmeans(dat[, -1], k) 
  withinVar <- c(withinVar, km$tot.withinss) 
} 
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. How many rows are in your `dat` data.frame? – MrFlick Apr 26 '20 at 22:01
  • dim(unique(dat[,-1])); just check if it is more than k? – StupidWolf Apr 26 '20 at 22:32
  • Welcome to Stack Overflow @Max Gress. Could you provide the `dat` data.frame? – vpz Apr 26 '20 at 22:37
  • imageId mountain tree prettySky person child animal virginiaTech food entertainment city country building 1 1 1 0 1 0 0 1 0 1 1 0 1 2 1 1 1 0 0 0 0 0 0 0 1 1 3 0 1 1 1 0 0 1 0 1 0 0 1 4 0 1 1 0 0 0 1 0 1 1 0 1 5 0 1 1 1 0 0 1 0 0 0 0 1 6 1 1 1 0 0 0 0 0 0 1 0 1 this is a sample of the first few rows of the data frame. The actual dimension of it is 31 observations to 13 variables. – Max Gress Apr 27 '20 at 00:29
  • The dimension of it is less then K. The dimension of that code is 27 observations to 12 variables. – Max Gress Apr 27 '20 at 00:32

0 Answers0