| lengths {BiocGenerics} | R Documentation |
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.
lengths(x, use.names=TRUE)
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 |
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.
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.
base::lengths for the default lengths
method.
showMethods for displaying a summary of the
methods defined for a given generic function.
selectMethod for getting the definition of
a specific method.
lengths,Vector-method in the S4Vectors
package for an example of a specific lengths method (defined
for Vector objects).
BiocGenerics for a summary of all the generics defined in the BiocGenerics package.
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