Divide a vector-like object into groups
split divides the data in a vector-like object x into the
groups defined by f.
NOTE: This man page is for the split methods defined in the
S4Vectors package. See ?base::split for the
default method (defined in the base package).
## S4 method for signature 'Vector,ANY' split(x, f, drop=FALSE, ...) ## S4 method for signature 'ANY,Vector' split(x, f, drop=FALSE, ...) ## S4 method for signature 'Vector,Vector' split(x, f, drop=FALSE, ...) ## S4 method for signature 'list,Vector' split(x, f, drop=FALSE, ...) splitAsList(x, f, drop=FALSE, ...) relistToClass(x)
x, f |
2 vector-like objects of the same length. |
drop |
Logical indicating if levels that do not occur should be dropped (if
|
... |
Extra arguments passed to any of the first 3 Extra arguments passed to the last Extra arguments passed to |
The first 3 split() methods just delegate to splitAsList().
The last split() method just does:
split(x, as.vector(f), drop=drop, ...)
splitAsList() is an S4 generic function. It is the workhorse
behind the first 3 split() methods above. It behaves like
base::split() except that it returns a List derivative
instead of an ordinary list. The exact class of this List
derivative depends only on the class of x and can be obtained
independently with relistToClass(x).
Note that relistToClass(x) is the opposite of elementType(y)
in the sense that the former returns the class of the result of relisting
(or splitting) x while the latter returns the class of the result
of unlisting (or unsplitting) y.
More formally, if x is an object that is relistable and y
a list-like object:
relistToClass(x) is class(relist(x, some_skeleton))
elementType(y) is class(unlist(y))Therefore, for any object x for which relistToClass(x)
is defined and returns a valid class,
elementType(new(relistToClass(x))) should return class(x).
## On an Rle object:
x <- Rle(101:105, 6:2)
split(x, c("B", "B", "A", "B", "A"))
## On a DataFrame object:
groups <- c("group1", "group2")
DF <- DataFrame(
a=letters[1:10],
i=101:110,
group=rep(factor(groups, levels=groups), c(3, 7))
)
split(DF, DF$group)
## Use splitAsList() if you need to split an ordinary vector into a
## List object:
split(letters, 1:2) # ordinary list
splitAsList(letters, 1:2) # List objectPlease choose more modern alternatives, such as Google Chrome or Mozilla Firefox.