length
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 NA
s
(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 (Formula) and that return a vector of type
double
(Matrix), even with non-integer values
(earlier versions of sets). Where a single double value is
returned that can be represented as an integer it is returned as a
length-one integer vector.
References
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
See Also
nchar
for counting the number of characters in character
vectors, lengths
for getting the length of every element
in a list.
Examples
library(base)
# NOT RUN {
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
# }
Community examples
The length of an atomic vector is simply the number of elements in it. ```{r} (v <- runif(10)) length(v) ``` The same is true of matrices. ```{r} (m <- matrix(1:30, 5, 6)) length(m) identical(length(m), nrow(m) * ncol(m)) ``` … and `array`s. ```{r} (a <- array(1:24, 2:4)) length(a) all.equal(length(a), prod(dim(a))) ``` For `list`s, the length is the number of top-level elements ```{r} (l <- list(a = 1, b = 2:4, c = 5:9, d = 10:16, e = 17:25)) length(l) ``` Be careful: for `data.frame`s, the length refers to the number of columns, not the total number of elements. ```{r} (d <- data.frame(x = letters[1:7], y = runif(7) > 0.5)) length(d) identical(length(d), ncol(d)) ``` formulae also have a length: 3 if there is a left-hand side, and 2 if not. ```{r} length(y ~ x1 + x2 + x3 + x4) length(~ x1 + x2 + x3 + x4) ``` Coerce the formulae to lists to see why. ```{r} as.list(y ~ x1 + x2 + x3 + x4) as.list(~ x1 + x2 + x3 + x4) ``` Functions always have length 1. ```{r} length(var) length(`c`) length(`if`) ``` So do names. ```{r} length(as.name("xyz")) ``` The length of a function `call` is 1 plus the number of arguments passed to the function. (Again, coerce to `list` to see why.) ```{r} (cl <- quote(mean(1:10, na.rm = TRUE))) length(cl) ``` The length of an expression is the number of calls, symbols or constants that you pass into it. If you pass multiple objects in braces, they only count as one item. ```{r} (ex <- expression( x, {y <- x ^ 2; y + 2}, mean(1:10, na.rm = TRUE), 1:5 )) length(ex) ``` External pointers always have length 1. ```{r} xptr <- xml2::read_xml("<foo><bar /></foo>")$node length(xptr) ```