Description Usage Arguments Value Examples
Split an array along a dimension
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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)
|
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
|
A list of arrays, or if a list of arrays was passed in, then a list of lists of arrays.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 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))
|
, , 1
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
, , 2
[,1] [,2] [,3]
[1,] 7 1 3
[2,] 8 2 4
, , 3
[,1] [,2] [,3]
[1,] 5 7 1
[2,] 6 8 2
, , 4
[,1] [,2] [,3]
[1,] 3 5 7
[2,] 4 6 8
[[1]]
[,1] [,2] [,3] [,4]
[1,] 1 7 5 3
[2,] 2 8 6 4
[[2]]
[,1] [,2] [,3] [,4]
[1,] 3 1 7 5
[2,] 4 2 8 6
[[3]]
[,1] [,2] [,3] [,4]
[1,] 5 3 1 7
[2,] 6 4 2 8
$a
, , 1
[,1] [,2]
[1,] 1 3
[2,] 2 4
, , 2
[,1] [,2]
[1,] 7 1
[2,] 8 2
, , 3
[,1] [,2]
[1,] 5 7
[2,] 6 8
, , 4
[,1] [,2]
[1,] 3 5
[2,] 4 6
$b
, , 1
[,1]
[1,] 5
[2,] 6
, , 2
[,1]
[1,] 3
[2,] 4
, , 3
[,1]
[1,] 1
[2,] 2
, , 4
[,1]
[1,] 7
[2,] 8
Loading required package: zeallot
Loading required package: magrittr
Loading required package: purrr
Attaching package: ‘purrr’
The following object is masked from ‘package:magrittr’:
set_names
List of 3
$ X: int [1:2, 1:3, 1:3] 1 4 11 14 21 24 31 34 41 44 ...
$ Y: num [1:2, 1:10] 1 0 0 0 0 0 0 1 0 0 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : NULL
.. ..$ : chr [1:10] "a" "b" "c" "d" ...
$ y: chr [1:2] "a" "d"
List of 3
$ X: int [1:6, 1:3, 1:3] 6 5 3 8 2 7 16 15 13 18 ...
$ Y: num [1:6, 1:10] 0 0 0 0 0 0 0 0 0 0 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : NULL
.. ..$ : chr [1:10] "a" "b" "c" "d" ...
$ y: chr [1:6] "f" "e" "c" "h" ...
List of 3
$ X: int [1:2, 1:3, 1:3] 10 9 20 19 30 29 40 39 50 49 ...
$ Y: num [1:2, 1:10] 0 0 0 0 0 0 0 0 0 0 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : NULL
.. ..$ : chr [1:10] "a" "b" "c" "d" ...
$ y: chr [1:2] "j" "i"
Loading required package: tibble
# A tibble: 10 x 2
y X
<chr> <list>
1 a <int[,3] [3 × 3]>
2 b <int[,3] [3 × 3]>
3 c <int[,3] [3 × 3]>
4 d <int[,3] [3 × 3]>
5 e <int[,3] [3 × 3]>
6 f <int[,3] [3 × 3]>
7 g <int[,3] [3 × 3]>
8 h <int[,3] [3 × 3]>
9 i <int[,3] [3 × 3]>
10 j <int[,3] [3 × 3]>
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.