bind-arrays: Bind arrays along a specified dimension

Description Usage Arguments Details Value Examples

Description

bind_as_* introduces a new dimension, such that each element in list_of_arrays corresponds to one index position along the new dimension in the returned array. bind_on_* binds all elements along an existing dimension, (meaning, the returned array has the same number of dimensions as each of the arrays in the list).

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
bind_as_dim(list_of_arrays, which_dim)

bind_as_rows(...)

bind_as_cols(...)

bind_on_dim(list_of_arrays, which_dim)

bind_on_rows(...)

bind_on_cols(...)

Arguments

list_of_arrays

a list of arrays. All arrays must be of the same dimension. NULL's in place of arrays are automatically dropped.

which_dim

Scalar integer specifying the index position of where to introduce the new dimension to introduce. Negative numbers count from the back. For example, given a 3 dimensional array, -1, is equivalent to 3, -2 to 2 and -3 to 1.

...

Arrays to be bound, specified individually or supplied as a single list

Details

bind_*_rows() is a wrapper for the common case of bind_*_dim(X, 1). bind_*_cols() is a wrapper for the common case of bind_*_dim(X, -1).

Value

An array, with one additional dimension.

Examples

 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
list_of_arrays <- replicate(10, array(1:8, dim = c(2,3,4)), FALSE)

dim(list_of_arrays[[1]])

# bind on a new dimension
combined_as <- bind_as_rows(list_of_arrays)
dim(combined_as)
dim(combined_as)[1] == length(list_of_arrays)

# each element in `list_of_arrays` corresponds to one "row"
# (i.e., one entry in along the first dimension)
for(i in seq_along(list_of_arrays))
  stopifnot(identical(combined_as[i,,,], list_of_arrays[[i]]))

# bind on an existing dimension
combined_on <- bind_on_rows(list_of_arrays)
dim(combined_on)
dim(combined_on)[1] == sum(sapply(list_of_arrays, function(x) dim(x)[1]))
identical(list_of_arrays[[1]], combined_on[1:2,,])
for (i in seq_along(list_of_arrays))
  stopifnot(identical(
    list_of_arrays[[i]], combined_on[ (1:2) + (i-1)*2,,]
  ))

# bind on any dimension
combined <- bind_as_dim(list_of_arrays, 3)
dim(combined)
for(i in seq_along(list_of_arrays))
   stopifnot(identical(combined[,,i,], list_of_arrays[[i]]))

Example output

[1] 2 3 4
[1] 10  2  3  4
[1] TRUE
[1] 20  3  4
[1] TRUE
[1] TRUE
[1]  2  3 10  4

listarrays documentation built on March 26, 2020, 6:10 p.m.