List-class: List objects

Description Construction Accessors Coercion Subsetting Author(s) See Also Examples


List objects are Vector objects with a "[[", elementType and elementNROWS method. The List class serves a similar role as list in base R.

It adds one slot, the elementType slot, to the two slots shared by all Vector objects.

The elementType slot is the preferred location for List subclasses to store the type of data represented in the sequence. It is designed to take a character of length 1 representing the class of the sequence elements. While the List class performs no validity checking based on elementType, if a subclass expects elements to be of a given type, that subclass is expected to perform the necessary validity checking. For example, the subclass IntegerList (defined in the IRanges package) has elementType = "integer" and its validity method checks if this condition is TRUE.

To be functional, a class that inherits from List must define at least a "[[" method (in addition to the minimum set of Vector methods).


List objects are typically constructed using one of the 3 following methods:


In the following code snippets, x is a List object.

length(x): Get the number of list elements in x.

names(x), names(x) <- value: Get or set the names of the elements in the List.

mcols(x, use.names=FALSE), mcols(x) <- value: Get or set the metadata columns. See Vector man page for more information.

elementType(x): Get the scalar string naming the class from which all elements must derive.

elementNROWS(x): Get the length (or nb of row for a matrix-like object) of each of the elements. Equivalent to sapply(x, NROW).

isEmpty(x): Returns a logical indicating either if the sequence has no elements or if all its elements are empty.


To List.

as(x, "List"): Converts a vector-like object into a List, usually a CompressedList derivative. One notable exception is when x is an ordinary list, in which case as(x, "List") returns a SimpleList derivative.

To explicitly request a SimpleList derivative, call as(x, "SimpleList").

See ?CompressedList (you might need to load the IRanges package first) and ?SimpleList for more information about the CompressedList and SimpleList representations.

From List. In the code snippets below, x is a List object.

as.list(x, ...), as(from, "list"): Turns x into an ordinary list.

unlist(x, recursive=TRUE, use.names=TRUE): Concatenates the elements of x into a single vector-like object (of class elementType(x))., row.names=NULL, optional=FALSE ,"value", use.outer.mcols=FALSE,, ...): Coerces a List to a data.frame. The result has the same length as unlisted x with two additional columns, group and group_name. group is an integer that indicates which list element the record came from. group_name holds the list name associated with each record; value is character by default and factor when is TRUE.

When use.outer.mcols is TRUE the metadata columns on the outer list elements of x are replicated out and included in the data.frame. List objects that unlist to a single vector (column) are given the column name 'value' by default. A custom name can be provided in

Splitting values in the resulting data.frame by the original groups in x should be done using the group column as the f argument to splitAsList. To relist data, use x as the skeleton argument to relist.


In the code snippets below, x is a List object.

x[i]: Return a new List object made of the list elements selected by subscript i. Subscript i can be of any type supported by subsetting of a Vector object (see Vector man page for the details), plus the following types: IntegerList, LogicalList, CharacterList, integer-RleList, logical-RleList, character-RleList, and IntegerRangesList. Those additional types perform subsetting within the list elements rather than across them.

x[i] <- value: Replacement version of x[i].

x[[i]]: Return the selected list element i, where i is an numeric or character vector of length 1.

x[[i]] <- value: Replacement version of x[[i]].

x$name, x$name <- value: Similar to x[[name]] and x[[name]] <- value, but name is taken literally as an element name.


P. Aboyoun and H. Pagès

See Also


showClass("List")  # shows only the known subclasses define in this package

Example output

Loading required package: stats4
Loading required package: BiocGenerics
Loading required package: parallel

Attaching package:BiocGenericsThe following objects are masked frompackage:parallel:

    clusterApply, clusterApplyLB, clusterCall, clusterEvalQ,
    clusterExport, clusterMap, parApply, parCapply, parLapply,
    parLapplyLB, parRapply, parSapply, parSapplyLB

The following objects are masked frompackage:stats:

    IQR, mad, sd, var, xtabs

The following objects are masked frompackage:base:

    anyDuplicated, append,, basename, cbind, colnames,
    dirname,, duplicated, eval, evalq, Filter, Find, get, grep,
    grepl, intersect, is.unsorted, lapply, Map, mapply, match, mget,
    order, paste, pmax,, pmin,, Position, rank,
    rbind, Reduce, rownames, sapply, setdiff, sort, table, tapply,
    union, unique, unsplit, which.max, which.min

Attaching package:S4VectorsThe following object is masked frompackage:base:


Virtual Class "List" [package "S4Vectors"]

Name:        elementType   elementMetadata          metadata
Class:         character DataFrame_OR_NULL              list

Class "Vector", directly
Class "list_OR_List", directly
Class "Annotated", by class "Vector", distance 2
Class "vector_OR_Vector", by class "Vector", distance 2

Known Subclasses: 
Class "SimpleList", directly
Class "TransposedDataFrame", directly
Class "HitsList", by class "SimpleList", distance 2
Class "DataFrame", by class "SimpleList", distance 2
Class "SelfHitsList", by class "SimpleList", distance 3
Class "SortedByQueryHitsList", by class "SimpleList", distance 3
Class "DFrame", by class "SimpleList", distance 3
Class "SortedByQuerySelfHitsList", by class "SimpleList", distance 4
Class "FilterRules", by class "SimpleList", distance 2

S4Vectors documentation built on Dec. 11, 2020, 2:02 a.m.