split_on_dim {listarrays} | R Documentation |
Split an array along a dimension
Description
Split an array along a dimension
Usage
split_on_dim(
X,
which_dim,
f = dimnames(X)[[which_dim]],
drop = FALSE,
depth = Inf
)
split_on_rows(X, f = rownames(X), drop = FALSE, depth = Inf)
split_on_cols(X, f = rownames(X), drop = FALSE, depth = Inf)
split_along_dim(X, which_dim, depth = Inf)
split_along_rows(X, depth = Inf)
split_along_cols(X, depth = Inf)
Arguments
X |
an array, or list of arrays. An atomic vector without a dimension
attribute is treated as a 1 dimensional array (Meaning, atomic vectors
without a dim attribute are only accepted if |
which_dim |
a scalar string or integer, specifying which dimension to
split along. Negative integers count from the back. If a string, it must
refer to a named dimension (e.g, one of |
f |
Specify how to split the dimension.
|
drop |
passed on to |
depth |
Scalar number, how many levels to recurse down. Set this if you
want to explicitly treat a list as a vector (that is, a one-dimensional
array). (You can alternatively set dim attributes with
|
Value
A list of arrays, or if a list of arrays was passed in, then a list of lists of arrays.
Examples
X <- array(1:8, c(2,3,4))
X
split_along_dim(X, 2)
# specify f as a factor, akin to base::split()
split_on_dim(X, 2, c("a", "a", "b"), drop = FALSE)
d <- c(10, 3, 3)
X <- array(1:prod(d), d)
y <- letters[1:10]
Y <- onehot(y)
# specify `f`` as relative partition sizes
if(require(zeallot) && require(magrittr) && require(purrr)) {
c(train, validate, test) %<-% {
list(X = X, Y = Y, y = y) %>%
shuffle_rows() %>%
split_on_rows(c(0.6, 0.2, 0.2)) %>%
transpose()
}
str(test)
str(train)
str(validate)
}
# with with array data in a data frame by splitting row-wise
if(require(tibble))
tibble(y, X = split_along_rows(X))