Split an array along its n-th dimension. The implementation was found here: https://stackoverflow.com/questions/20198751/three-dimensional-array-to-list
Examples
# Define an array
A <- array(1:8, dim = c(2, 2, 2), dimnames = list(
paste0("x", 1:2), paste0("y", 1:2), paste0("z", 1:2)
))
splitAlongDim(A, 1)
#> $x1
#> z1 z2
#> y1 1 5
#> y2 3 7
#>
#> $x2
#> z1 z2
#> y1 2 6
#> y2 4 8
#>
splitAlongDim(A, 2)
#> $y1
#> z1 z2
#> x1 1 5
#> x2 2 6
#>
#> $y2
#> z1 z2
#> x1 3 7
#> x2 4 8
#>
splitAlongDim(A, 3)
#> $z1
#> y1 y2
#> x1 1 3
#> x2 2 4
#>
#> $z2
#> y1 y2
#> x1 5 7
#> x2 6 8
#>