\HeaderA{length}{Length of an Object}{length}
\aliasA{length<\Rdash}{length}{length<.Rdash.}
\methaliasA{length<\Rdash.factor}{length}{length<.Rdash..factor}
\keyword{attribute}{length}
\begin{Description}\relax
Get or set the length of vectors (including lists) and factors, and of
any other \R{} object for which a method has been defined.
\end{Description}
\begin{Usage}
\begin{verbatim}
length(x)
length(x) <- value
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{x}] an \R{} object.  For replacement, a vector or factor.
\item[\code{value}] an integer.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
Both functions are generic: you can write methods to handle specific
classes of objects, see \LinkA{InternalMethods}{InternalMethods}.  \code{length<-} has a
\code{"factor"} method.

The replacement form can be used to reset the length of a vector.  If
a vector is shortened, extra values are discarded and when a vector is
lengthened, it is padded out to its new length with \code{\LinkA{NA}{NA}}s
(\code{nul} for raw vectors).
\end{Details}
\begin{Value}
The default method currently returns an \code{\LinkA{integer}{integer}} of
length 1.  Since this may change in the future and may
differ for other methods, programmers should not rely on it.
(Should the length exceed the maximum representable integer, it is
returned as \code{NA}.)

For vectors (including lists) and factors the length is the number of
elements.  For an environment it is the number of objects in the
environment, and \code{NULL} has length 0.  For expressions and
pairlists (including language objects and dotlists) it is the length
of the pairlist chain.  All other objects (including functions) have
length one: note that for functions this differs from S.

The replacement form removes all the attributes of \code{x} except its
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{nchar} for counting the number of characters in
character vectors.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
length(diag(4))# = 16 (4 x 4)
length(options())# 12 or more
length(y ~ x1 + x2 + x3)# 3
length(expression(x, {y <- x^2; y+2}, x^y)) # 3

## from example(warpbreaks)
require(stats)

fm1 <- lm(breaks ~ wool * tension, data = warpbreaks)
length(fm1$call) # 3, lm() and two arguments.
length(formula(fm1)) # 3, ~ lhs rhs
\end{ExampleCode}
\end{Examples}

