collapse provides the following set of functions to work with lists of R objects:
Search and Identification
is.regular checks whether an R object is either atomic or a list. A (nested) list composed of regular objects at each level of the list-tree is unlistable to an atomic vector, checked by is.unlistable.
ldepth determines the level of nesting of the list (i.e. the maximum number of nodes of the list-tree).
has_elem searches elements in a list using element names, regular expressions applied to element names, or a function applied to the elements, and returns TRUE if any matches were found.
Subsetting
atomic_elem examines the top-level of a list and returns a sublist with the atomic elements. Conversely list_elem returns the sublist of elements which are themselves lists or list-like objects.
reg_elem and irreg_elem are recursive versions of the former. reg_elem extracts the regular part of the list-tree (leading to atomic elements in the final nodes), while irreg_elem extracts the 'irregular' part of the list tree leading to non-atomic elements in the final nodes. (Tipp: try calling both on an lm object). Naturally for all lists l, is.unlistable(reg_elem(l)) evaluates to TRUE...
get_elem extracts elements from a list using element names, regular expressions applied to element names, a function applied to the elements, or element-indices used to subset the lowest-level sub-lists. by default the result is presented as a simplified list containing all matching elements. With the keep.tree option however get_elem can also be used to subset lists i.e. maintain the full tree but cut off non-matching branches.
Apply Functions
rapply2d is a recursive version of base::lapply with two key differences to base::rapply: (1) Data frames are considered as atomic objects, not as (sub-)lists, and (2) the result is not simplified.
Unlisting / Row-Binding
unlist2d efficiently unlists unlistable lists in 2-dimensions and creates a data.frame (or data.table) representation of the list (unlike base::unlist which returns an atomic vector). This is done by recursively flattening and row-binding R objects in the list (using data.table::rbindlist) while creating identifier columns for each level of the list-tree and (optionally) saving the row-names of the objects in a separate column. unlist2d can thus also be understood as a recursive generalization of do.call(rbind, l), for lists of vectors, data.frames, arrays or heterogeneous objects.
| Function | Description | is.regular | |
| function(x) is.atomic(x) || is.list(x) | is.unlistable | ||
| checks if list is unlistable | ldepth | level of nesting / maximum depth of list-tree | |
| has_elem | checks if list contains a certain element | get_elem | |
| subset list / extract certain elements | get_elem | ||
| subset list / extract certain elements | reg_elem | subset / extract regular part of list | |
| irreg_elem | subset / extract non-regular part of list | atomic_elem | |
| top-level subset atomic elements | list_elem | ||
| top-level subset list/list-like elements | rapply2d | recursively apply functions to lists of data objects | |
| unlist2d | recursively unlist/row-bind lists of data objects in 2D, to data.frameor data.table | Function |