\Header{codes}{Factor Codes}
\methalias{codes.factor}{codes}
\methalias{codes.ordered}{codes}
\alias{codes<\Rdash}{codes}
\keyword{category}{codes}
\keyword{classes}{codes}
\begin{Description}\relax
This (generic) function returns a numeric coding of a
factor. It can also be used to assign to a factor using the coded
form.\end{Description}
\begin{Usage}
\begin{verbatim}
codes(x)
codes(x) <- value
\end{verbatim}
\end{Usage}
\begin{Value}
For an ordered factor, it returns the internal coding (1 for
the lowest group, 2 for the second lowest, etc.).

For an unordered
factor, an alphabetical ordering of the levels is assumed, i.e the
level that is coded 1 is the one whose name is sorted first according
to the prevailing collating sequence. \bold{Warning:} the sort order
may well depend on the locale, and should not be assumed to be ASCII.\end{Value}
\begin{Note}\relax
Normally \code{codes} is not the appropriate function to use with an
unordered factor. Use \code{\Link{unclass}} or
\code{\Link{as.numeric}} to extract the codes used in the internal
representation of the factor, as these do not assume that the codes
are sorted.\end{Note}
\begin{SeeAlso}\relax
\code{\Link{factor}}, \code{\Link{levels}}, \code{\Link{nlevels}}.\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
codes(rep(factor(c(20,10)),3))

x <- gl(3,5)
codes(x)[3] <- 2
x

data(esoph)
( ag <- esoph$alcgp[12:1] )
codes(ag)

codes(factor(1:10)) # BEWARE!
\end{ExampleCode}
\end{Examples}

