Movatterモバイル変換


[0]ホーム

URL:


rdrr.io

lengths: Lengths of List or Vector Elements

lengthsR Documentation

Lengths of List or Vector Elements

Description

Get the length of each element of alist or atomicvector (is.atomic) as an integer or numeric vector.

Usage

lengths(x, use.names = TRUE)

Arguments

x

alist, list-like such as anexpression or an atomic vector (for which the resultis trivial).

use.names

logical indicating if the result should inherit thenames fromx.

Details

This function loops overx and returns a compatible vectorcontaining the length of each element inx. Effectively,length(x[[i]]) is called for alli, so any methods onlength are considered.

lengths is generic: you can write methods to handlespecific classes of objects, see InternalMethods.

Value

A non-negativeinteger of lengthlength(x),except when any element has a length of more than2^31 - 1 elements, when it returns a double vector.Whenuse.names is true, the names are taken from the names onx, if any.

Note

One raison d'être oflengths(x) is its use as amore efficient version ofsapply(x, length) and similar*apply calls tolength. This is the reason whyx may be an atomic vector, even thoughlengths(x) istrivial in that case.

See Also

length for getting the length of anyR object.

Examples

require(stats)## summarize by monthl <- split(airquality$Ozone, airquality$Month)avgOz <- lapply(l, mean, na.rm=TRUE)## merge resultairquality$avgOz <- rep(unlist(avgOz, use.names=FALSE), lengths(l))## but this is safer and cleaner, but can be slowerairquality$avgOz <- unsplit(avgOz, airquality$Month)## should always be true, except when a length does not fit in 32 bitsstopifnot(identical(lengths(l), vapply(l, length, integer(1L))))## empty lists are not a problemx <- list()stopifnot(identical(lengths(x), integer()))## nor are "list-like" expressions:lengths(expression(u, v, 1+ 0:9))## and we should dispatch to length methodsf <- c(rep(1, 3), rep(2, 6), 3)dates <- split(as.POSIXlt(Sys.time() + 1:10), f)stopifnot(identical(lengths(dates), vapply(dates, length, integer(1L))))

What can we improve?

R Package Documentation

Browse R Packages

We want your feedback!

Note that we can't provide technical support on individual packages. You should contact the package authors for that.

 
Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, readEmbedding Snippets.

Close

[8]ページ先頭

©2009-2026 Movatter.jp