\HeaderA{cutree}{Cut a tree into groups of data}{cutree}
\keyword{multivariate}{cutree}
\keyword{cluster}{cutree}
\begin{Description}\relax
Cuts a tree, e.g., as resulting from \code{\LinkA{hclust}{hclust}}, into several
groups either by specifying the desired number(s) of groups or the cut
height(s).
\end{Description}
\begin{Usage}
\begin{verbatim}
cutree(tree, k = NULL, h = NULL)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{tree}] a tree as produced by \code{\LinkA{hclust}{hclust}}. \code{cutree()}
only expects a list with components \code{merge}, \code{height}, and
\code{labels}, of appropriate content each.
\item[\code{k}] an integer scalar or vector with the desired number of groups
\item[\code{h}] numeric scalar or vector with heights where the tree should
be cut.
\end{ldescription}

At least one of \code{k} or \code{h} must be specified, \code{k}
overrides \code{h} if both are given.
\end{Arguments}
\begin{Value}
\code{cutree} returns a vector with group memberships if \code{k} or
\code{h} are scalar, otherwise a matrix with group meberships is returned
where each column corresponds to the elements of \code{k} or \code{h},
respectively (which are also used as column names).
\end{Value}
\begin{References}\relax
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
\emph{The New S Language}.
Wadsworth \& Brooks/Cole.
\end{References}
\begin{SeeAlso}\relax
\code{\LinkA{hclust}{hclust}}, \code{\LinkA{dendrogram}{dendrogram}} for cutting trees themselves.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
hc <- hclust(dist(USArrests))

cutree(hc, k=1:5)#k = 1 is trivial
cutree(hc, h=250)

## Compare the 2 and 3 grouping:
g24 <- cutree(hc, k = c(2,4))
table(g24[,"2"], g24[,"4"])
\end{ExampleCode}
\end{Examples}

