# length

0th

Percentile

##### Length of an Object

Get or set the length of vectors (including lists) and factors, and of any other R object for which a method has been defined.

Keywords
attribute
##### Usage
length(x)
length(x) <- value
##### Arguments
x
an R object. For replacement, a vector or factor.
value
a non-negative integer or double (which will be rounded down).
##### Details

Both functions are generic: you can write methods to handle specific classes of objects, see InternalMethods. length<- has a "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 NAs (nul for raw vectors).

Both are primitive functions.

##### Value

The default method for length currently returns a non-negative integer of length 1, except for vectors of more than $2^31 - 1$ elements, when it returns a double.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 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 x except its names, which are adjusted (and if necessary extended by "").

##### Warning

Package authors have written methods that return a result of length other than one (\href{https://CRAN.R-project.org/package=#1}{\pkg{#1}}FormulaFormula) and that return a vector of type double (\href{https://CRAN.R-project.org/package=#1}{\pkg{#1}}MatrixMatrix), even with non-integer values (earlier versions of \href{https://CRAN.R-project.org/package=#1}{\pkg{#1}}setssets). Where a single double value is returned that can be represented as an integer it is returned as a length-one integer vector. As from R 3.0.0, lengths can be returned as double in base R.

##### References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.

nchar for counting the number of characters in character vectors, lengths for getting the length of every element in a list.