rsplit | R Documentation |
rsplit
(recursively) splits a vector, matrix or data frame into subsets according to combinations of (multiple) vectors / factors and returns a (nested) list. If flatten = TRUE
, the list is flattened yielding the same result as split
. rsplit
is implemented as a wrapper around gsplit
, and significantly faster than split
.
rsplit(x, ...)
## Default S3 method:
rsplit(x, fl, drop = TRUE, flatten = FALSE, use.names = TRUE, ...)
## S3 method for class 'matrix'
rsplit(x, fl, drop = TRUE, flatten = FALSE, use.names = TRUE,
drop.dim = FALSE, ...)
## S3 method for class 'data.frame'
rsplit(x, by, drop = TRUE, flatten = FALSE, cols = NULL,
keep.by = FALSE, simplify = TRUE, use.names = TRUE, ...)
x |
a vector, matrix, data.frame or list like object. |
fl |
a |
by |
data.frame method: Same as |
drop |
logical. |
flatten |
logical. If |
use.names |
logical. |
drop.dim |
logical. |
cols |
data.frame method: Select columns to split using a function, column names, indices or a logical vector. Note: |
keep.by |
logical. If a formula is passed to |
simplify |
data.frame method: Logical. |
... |
further arguments passed to |
a (nested) list containing the subsets of x
.
gsplit
, rapply2d
, unlist2d
, List Processing, Collapse Overview
rsplit(mtcars$mpg, mtcars$cyl)
rsplit(mtcars, mtcars$cyl)
rsplit(mtcars, mtcars[.c(cyl, vs, am)])
rsplit(mtcars, ~ cyl + vs + am, keep.by = TRUE) # Same thing
rsplit(mtcars, ~ cyl + vs + am)
rsplit(mtcars, ~ cyl + vs + am, flatten = TRUE)
rsplit(mtcars, mpg ~ cyl)
rsplit(mtcars, mpg ~ cyl, simplify = FALSE)
rsplit(mtcars, mpg + hp ~ cyl + vs + am)
rsplit(mtcars, mpg + hp ~ cyl + vs + am, keep.by = TRUE)
# Split this sectoral data, first by Variable (Emloyment and Value Added), then by Country
GGDCspl <- rsplit(GGDC10S, ~ Variable + Country, cols = 6:16)
str(GGDCspl)
# The nested list can be reassembled using unlist2d()
head(unlist2d(GGDCspl, idcols = .c(Variable, Country)))
rm(GGDCspl)
# Another example with mtcars (not as clean because of row.names)
nl <- rsplit(mtcars, mpg + hp ~ cyl + vs + am)
str(nl)
unlist2d(nl, idcols = .c(cyl, vs, am), row.names = "car")
rm(nl)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.