\Header{kmeans}{K-Means Clustering}
\keyword{multivariate}{kmeans}
\keyword{cluster}{kmeans}
\begin{Usage}
\begin{verbatim}
kmeans(x, centers, iter.max=10)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\texttt{x}] A data frame or matrix of data.

\item[\texttt{centers}] Either the number of clusters or a set of initial cluster centers.
If the first, a random set of rows in \texttt{x} are chosen as the initial
centers.

\item[\texttt{iter.max}] The maximum number of iterations allowed.

\end{ldescription}
\end{Arguments}
\begin{Description}
The data given by \texttt{x} is clustered by the k-Means algorithm.
This algorithm works by repeatedly moving all cluster centers
to the mean of their Voronoi sets.

The algorithm stops, if no cluster center has changed during the last
iteration or the maximum number of iterations (given by
\texttt{iter.max}) is reached.\end{Description}
\begin{Value}
A list with components:

\begin{ldescription}
\item[\texttt{cluster}] A vector of integers indicating the cluster to which each point is
allocated.

\item[\texttt{centers}] A matrix of cluster centres.

\item[\texttt{withinss}] The within-cluster sum of squares for each cluster.

\item[\texttt{size}] The number of points in each cluster

\end{ldescription}
\end{Value}
\begin{References}
Hartigan,  J.A. and Wong, M.A. (1979).
A K-means clustering algorithm.
Applied Statistics 
\bold{28}, 100-108.\end{References}
\begin{Examples}
\begin{ExampleCode}
# a 2-dimensional example
x<-rbind(matrix(rnorm(100,sd=0.3),ncol=2),
         matrix(rnorm(100,mean=1,sd=0.3),ncol=2))
cl<-kmeans(x,2,20)
plot(x,col=cl$cluster)
points(cl$centers,col=1:2,pch=8)
\end{ExampleCode}
\end{Examples}

