\Header{gl}{Generate Factor Levels}
\keyword{category}{gl}
\keyword{arith}{gl}
\begin{Description}\relax
Generate factors by specifying the pattern of their levels.\end{Description}
\begin{Usage}
\begin{verbatim}
gl(n, k, length = n*k, labels = 1:n, ordered = FALSE)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{n}] an integer giving the number of levels.
\item[\code{k}] an integer giving the number of replications.
\item[\code{length}] an integer giving the length of the result.
\item[\code{labels}] an optional vector of labels for the resulting factor
levels.
\item[\code{ordered}] a logical indicating whether the result should be
ordered or not.
\end{ldescription}
\end{Arguments}
\begin{Value}
The result has levels from \code{1} to \code{n} with each value
replicated in groups of length \code{k} out to a total length of
\code{length}.

\code{gl} is modelled on the \emph{GLIM} function of the same name.\end{Value}
\begin{SeeAlso}\relax
The underlying \code{\Link{factor}(.)}.\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
# First control, then treatment:
gl(2,8, label=c("Control","Treat"))
# 20 alternating 1s and 2s
gl(2, 1, 20)
# alternating pairs of 1s and 2s
gl(2, 2, 20)

\end{ExampleCode}
\end{Examples}

