\HeaderA{divideUp}{Partition data into mutiple nearly equal subsets}{divideUp}
\keyword{arith}{divideUp}
\begin{Description}\relax
Randomly partition data into nearly equal subsets. If
\code{balanced=TRUE} the requirement is imposed that the subsets
should as far as possible be balanced with respect to a classifying
factor.  The multiple sets are suitable for use for determining the
folds in a cross-validation.
\end{Description}
\begin{Usage}
\begin{verbatim}
divideUp(cl, nset = 2, seed = NULL, balanced=TRUE)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{cl}] classifying factor
\item[\code{nset}] number of subsets into which to partition data
\item[\code{seed}] set the seed, if required, in order to obtain
reproducible results
\item[\code{balanced}] logical: should subsets be as far as possible balanced
with respect to the classifying factor?
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
\end{Details}
\begin{Value}
a set of indices that identify the \code{nset} subsets
\end{Value}
\begin{Note}\relax
\end{Note}
\begin{Author}\relax
John Maindonald
\end{Author}
\begin{References}\relax
\end{References}
\begin{SeeAlso}\relax
~~objects to See Also as \code{\LinkA{help}{help}}, ~~~
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
foldid <- divideUp(cl=rep(1:3, c(17,14,8)), nset=10)
table(rep(1:3, c(17,14,8)), foldid)
foldid <- divideUp(cl=rep(1:3, c(17,14,8)), nset=10,
            balanced=FALSE)
table(rep(1:3, c(17,14,8)), foldid)

## The function is currently defined as
function(cl = rep(1:3, c(7, 4, 8)), nset=2, seed=NULL, balanced=TRUE){
    if(!is.null(seed))set.seed(seed)
    if(balanced){
      ord <- order(cl)
      ordcl <- cl[ord]
      gp0 <- rep(sample(1:nset), length.out=length(cl))
      gp <- unlist(split(gp0,ordcl), function(x)sample(x))
      gp[ord] <- gp
    } else
    gp <- sample(rep(1:nset, length.out=length(cl)))
    as.vector(gp)
  }
\end{ExampleCode}
\end{Examples}

