lengths: Lengths of the list elements of a list-like object

Description Usage Arguments Value Note See Also Examples

Description

Get the length of each list element of a list-like object.

NOTE: This man page is for the lengths S4 generic function defined in the BiocGenerics package. See ?base::lengths for the default method (defined in the base package). Bioconductor packages can define specific methods for list-like objects not supported by the default method.

Usage

1
lengths(x, use.names=TRUE)

Arguments

x

A list-like object. Can also be a vector-like object that is not list-like, in which case the result is trivial.

use.names

See ?base::lengths for a description of this argument.

Value

See ?base::lengths for the value returned by the default method.

Specific methods defined in Bioconductor packages should behave as consistently as possible with the default method.

Note

IMPORTANT: The default method (base::lengths) is equivalent to sapply(x, length). However, because the lengths method for Vector objects is currently defined as an alias for S4Vectors::elementNROWS, it's equivalent to sapply(x, NROW), not to sapply(x, length).

This makes a difference if x has array-like list elements. See ?base::NROW for the difference between length() and NROW(). This difference is illustrated in the Examples section below.

This is a temporary situation that will be addressed in BioC 3.3.

See Also

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
lengths  # note the dispatch on the 'x' arg only
showMethods("lengths")
selectMethod("lengths", "ANY")  # the default method

library(S4Vectors)
showMethods("lengths")
selectMethod("lengths", "Vector")  # the "lengths" method for Vector
                                   # objects

## Difference between default method and method for Vector objects:
groups <- c("group1", "group2")
df <- data.frame(
    a=letters[1:10],
    i=101:110,
    group=rep(factor(groups, levels=groups), c(6, 4))
)
x1 <- split(df, df$group)
x2 <- split(DataFrame(df), df$group)
lengths(x1)  # dispatch on default method
lengths(x2)  # dispatch on method for Vector objects

nturaga/BiocGenerics documentation built on May 7, 2019, 11:15 a.m.