[R] kmeans: number of cluster centres must lie between 1 and nrow(x)

4,383 views
Skip to first unread message

Feng Li

unread,
Feb 1, 2011, 2:27:04 PM2/1/11
to r-h...@r-project.org
Dear R,

Can't I cluster a dataset into k clusters where k is exactly the number of
observations? I have version 12.2 installed. See this example

> a <- matrix(1:100, 20)
> kmeans(a, 20)
Error: number of cluster centres must lie between 1 and nrow(x)

This is a bit ad-hoc but I known R from version 2.12 allows number of
clusters to be one. So I guess allowing number of clusters to be nrow(x)
should be also possible in the future release?


Feng

--
Feng Li
Department of Statistics
Stockholm University
106 91 Stockholm, Sweden
http://feng.li/

[[alternative HTML version deleted]]

______________________________________________
R-h...@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Feng Li

unread,
Feb 2, 2011, 6:15:49 AM2/2/11
to Rafael Björk, r-h...@r-project.org
Thank you for the suggestion and it is exactly as you said only one
observation in each cluster. I know I can avoid this anyway and I am just
out of curiosity of the error.

I am writing a special algorithm to cluster some datasets with different
numbers of observations. For some particular datasets, there is only one
observation(e.g. people died of a rare disease). kmeans() thus will not work
at this situation.


Feng

2011/2/2 Rafael Björk <rafael...@gmail.com>

> If you change the algorithm, the function allow you to do this:
>
> > kmeans(a, 20,algorithm="Lloyd")
>
> Maybe i'm missing something here, but why would you want to create as many
> clusters as there are observations? Won't the outcome just be one
> observation in each cluster?

Reply all
Reply to author
Forward
0 new messages