splitare 2 common ways of grouping the elements of a vector-like object into a list-like object. The IRanges and S4Vectors packages define
splitmethods that operate on a Vector object and return a List object. Note that the
splitmethods defined in S4Vectors delegate to the
splitAsListfunction defined in IRanges and documented below.
split both impose restrictions on
the kind of grouping that they support (e.g. every element in the input
object needs to go in a group and can only go in one group), the
IRanges package introduces the
extractList generic function
for performing arbitrary groupings.
## relist() ## --------"relist"(flesh, skeleton)"relist"(flesh, skeleton)## splitAsList() ## -------------splitAsList(x, f, drop=FALSE, ...)## extractList() ## -------------extractList(x, i)## regroup() ## ---------regroup(x, g)
skeletonmatters. Its exact content is ignored.
fis a factor).
skeleton, the content here matters (see Details section below). Note that
ican be a Ranges object (a particular type of list-like object), and, in that case,
extractListis particularly fast (this is a common use case).
ggroups the elements of
relistmethods behave like
utils::relistexcept that they return a List object. If
skeletonhas names, then they are propagated to the returned value.
base::splitexcept that the former returns a List object instead of an ordinary list.
extractListreturns a list-like object parallel to
iand with the same "shape" as
i(i.e. same element lengths). If
ihas names, then they are propagated to the returned value.All these functions return a list-like object where the list elements have the same class as
relistToClassgives the exact class of the returned object.
extractListhave in common that they return a list-like object where each list element has the same class as the original vector-like object. Thus they need to be able to select the appropriate List concrete subclass to use for this returned value. This selection is performed by
relistToClassand is based only on the class of the original object.
extractList(x, i) is equivalent to:
relist(x[unlist(i)], i)An exception is made when
xis a data-frame-like object. In that case
xis subsetted along the rows, that is,
extractList(x, i)is equivalent to:
relist(x[unlist(i), ], i)This is more or less how the default method is implemented, except for some optimizations when
iis a Ranges object.
splitAsList) can be seen as
special cases of
relist(flesh, skeleton) is equivalent to extractList(flesh, PartitioningByEnd(skeleton))
split(x, f) is equivalent to
extractList(x, split(seq_along(f), f))
It is good practise to use
extractList only for cases not covered
split. Whenever possible, using
split is preferred as they will always perform more efficiently.
In addition their names carry meaning and are familiar to most R
users/developers so they'll make your code easier to read/understand.
Note that the transformation performed by
is always reversible (via
but not the transformation performed by
extractList (in general).
regroup function splits the elements of
into a list according to the grouping
g. Each element of
unlist(x) inherits its group from its parent element of
regroup is different from
x is already grouped, and the goal is to
relistfunctions in the base and utils packages, respectively.
unsplitfunctions in the base package.
splitmethods defined in the S4Vectors package.
relistToClassis documented in the man page for List objects.
## On an Rle object: x <- Rle(101:105, 6:2) i <- IRanges(6:10, 16:12, names=letters[1:5]) extractList(x, i) ## On a DataFrame object: df <- DataFrame(X=x, Y=LETTERS[1:20]) extractList(df, i)
Run the code above in your browser using DataCamp Workspace