list-processing {collapse} | R Documentation |
List Processing
Description
collapse provides the following set of functions to efficiently work with lists of R objects:
-
Search and Identification
-
is_unlistable
checks whether a (nested) list is composed of atomic objects in all final nodes, and thus unlistable to an atomic vector usingunlist
. -
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 returnsTRUE
if any matches were found.
-
-
Subsetting
-
atomic_elem
examines the top-level of a list and returns a sublist with the atomic elements. Converselylist_elem
returns the sublist of elements which are themselves lists or list-like objects. -
reg_elem
andirreg_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, whileirreg_elem
extracts the 'irregular' part of the list tree leading to non-atomic elements in the final nodes. (Tip: try calling both on anlm
object). Naturally for all listsl
,is_unlistable(reg_elem(l))
evaluates toTRUE
. -
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 thekeep.tree
option howeverget_elem
can also be used to subset lists i.e. maintain the full tree but cut off non-matching branches.
-
-
Splitting and Transposition
-
rsplit
recursively splits a vector or data frame into subsets according to combinations of (multiple) vectors / factors - by default returning a (nested) list. Ifflatten = TRUE
, the list is flattened yielding the same result assplit
.rsplit
is also faster thansplit
, particularly for data frames. -
t_list
efficiently transposes nested lists of lists, such as those obtained from splitting a data frame by multiple variables usingrsplit
.
-
-
Apply Functions
-
Unlisting / Row-Binding
-
unlist2d
efficiently unlists unlistable lists in 2-dimensions and creates a data frame (or data.table) representation of the list. This is done by recursively flattening and row-binding R objects in the list 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 ofdo.call(rbind, l)
, for lists of vectors, data frames, arrays or heterogeneous objects. A simpler version for non-recursive row-binding lists of lists / data.frames, is also available byrowbind
.
-
Table of Functions
Function | Description | |
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 | |
atomic_elem | Top-level subset atomic elements | |
list_elem | Top-level subset list/list-like elements | |
reg_elem | Recursive version of atomic_elem : Subset / extract 'regular' part of list |
|
irreg_elem | Subset / extract non-regular part of list | |
rsplit | Recursively split vectors or data frames / lists | |
t_list | Transpose lists of lists | |
rapply2d | Recursively apply functions to lists of data objects | |
unlist2d | Recursively unlist/row-bind lists of data objects in 2D, to data frame or data.table | |
rowbind | Non-recursive binding of lists of lists / data.frames. | |