I couldn't find anything in the system library to do what you're asking, but the `abind`

package has a function called `asub`

that I think should do what you're asking! It essentially builds the subsetting calls for you. I actually found it already installed on my system; my guess is that it's a popular dependency (**EDIT:** turns out the `stars`

package, which is a current effort to improve higher-dimensional spatiotemporal array functionality in R, uses this package).

Here're some examples. It's pretty flexible!

```
library(abind)
#> Warning: package 'abind' was built under R version 3.5.2
Y <- array(data = 1:120, dim = c(3, 5, 2, 4))
# index (idx) goes first, then dimension (dims)
asub(Y, 3, 2)
#> , , 1
#>
#> [,1] [,2]
#> [1,] 7 22
#> [2,] 8 23
#> [3,] 9 24
#>
#> , , 2
#>
#> [,1] [,2]
#> [1,] 37 52
#> [2,] 38 53
#> [3,] 39 54
#>
#> , , 3
#>
#> [,1] [,2]
#> [1,] 67 82
#> [2,] 68 83
#> [3,] 69 84
#>
#> , , 4
#>
#> [,1] [,2]
#> [1,] 97 112
#> [2,] 98 113
#> [3,] 99 114
dim(asub(Y, 3, 2))
#> [1] 3 2 4
# add drop = FALSE to avoid collapsing a dimension when you
# only specify a single index
asub(Y, 3, 2, drop = FALSE)
#> , , 1, 1
#>
#> [,1]
#> [1,] 7
#> [2,] 8
#> [3,] 9
#>
#> , , 2, 1
#>
#> [,1]
#> [1,] 22
#> [2,] 23
#> [3,] 24
#>
#> , , 1, 2
#>
#> [,1]
#> [1,] 37
#> [2,] 38
#> [3,] 39
#>
#> , , 2, 2
#>
#> [,1]
#> [1,] 52
#> [2,] 53
#> [3,] 54
#>
#> , , 1, 3
#>
#> [,1]
#> [1,] 67
#> [2,] 68
#> [3,] 69
#>
#> , , 2, 3
#>
#> [,1]
#> [1,] 82
#> [2,] 83
#> [3,] 84
#>
#> , , 1, 4
#>
#> [,1]
#> [1,] 97
#> [2,] 98
#> [3,] 99
#>
#> , , 2, 4
#>
#> [,1]
#> [1,] 112
#> [2,] 113
#> [3,] 114
dim(asub(Y, 3, 2, drop = FALSE))
#> [1] 3 1 2 4
# you can specify a range of indices too...
asub(Y, 3:5, 2)
#> , , 1, 1
#>
#> [,1] [,2] [,3]
#> [1,] 7 10 13
#> [2,] 8 11 14
#> [3,] 9 12 15
#>
#> , , 2, 1
#>
#> [,1] [,2] [,3]
#> [1,] 22 25 28
#> [2,] 23 26 29
#> [3,] 24 27 30
#>
#> , , 1, 2
#>
#> [,1] [,2] [,3]
#> [1,] 37 40 43
#> [2,] 38 41 44
#> [3,] 39 42 45
#>
#> , , 2, 2
#>
#> [,1] [,2] [,3]
#> [1,] 52 55 58
#> [2,] 53 56 59
#> [3,] 54 57 60
#>
#> , , 1, 3
#>
#> [,1] [,2] [,3]
#> [1,] 67 70 73
#> [2,] 68 71 74
#> [3,] 69 72 75
#>
#> , , 2, 3
#>
#> [,1] [,2] [,3]
#> [1,] 82 85 88
#> [2,] 83 86 89
#> [3,] 84 87 90
#>
#> , , 1, 4
#>
#> [,1] [,2] [,3]
#> [1,] 97 100 103
#> [2,] 98 101 104
#> [3,] 99 102 105
#>
#> , , 2, 4
#>
#> [,1] [,2] [,3]
#> [1,] 112 115 118
#> [2,] 113 116 119
#> [3,] 114 117 120
dim(asub(Y, 3, 2))
#> [1] 3 2 4
# ... as well as several dimensions.
# but in this case, you need to provide the indices for each
# dimension separately, as a list
asub(Y, list(3, 3:5), 1:2)
#> , , 1
#>
#> [,1] [,2]
#> [1,] 9 24
#> [2,] 12 27
#> [3,] 15 30
#>
#> , , 2
#>
#> [,1] [,2]
#> [1,] 39 54
#> [2,] 42 57
#> [3,] 45 60
#>
#> , , 3
#>
#> [,1] [,2]
#> [1,] 69 84
#> [2,] 72 87
#> [3,] 75 90
#>
#> , , 4
#>
#> [,1] [,2]
#> [1,] 99 114
#> [2,] 102 117
#> [3,] 105 120
dim(asub(Y, list(3, 3:5), 1:2))
#> [1] 3 2 4
# remember, drop = FALSE keeps the original dimensionality
asub(Y, list(3, 3:5), 1:2, drop = FALSE)
#> , , 1, 1
#>
#> [,1] [,2] [,3]
#> [1,] 9 12 15
#>
#> , , 2, 1
#>
#> [,1] [,2] [,3]
#> [1,] 24 27 30
#>
#> , , 1, 2
#>
#> [,1] [,2] [,3]
#> [1,] 39 42 45
#>
#> , , 2, 2
#>
#> [,1] [,2] [,3]
#> [1,] 54 57 60
#>
#> , , 1, 3
#>
#> [,1] [,2] [,3]
#> [1,] 69 72 75
#>
#> , , 2, 3
#>
#> [,1] [,2] [,3]
#> [1,] 84 87 90
#>
#> , , 1, 4
#>
#> [,1] [,2] [,3]
#> [1,] 99 102 105
#>
#> , , 2, 4
#>
#> [,1] [,2] [,3]
#> [1,] 114 117 120
dim(asub(Y, list(3, 3:5), 1:2), drop = FALSE)
#> Error in dim(asub(Y, list(3, 3:5), 1:2), drop = FALSE): 2 arguments passed to 'dim' which requires 1
```

Created on 2019-01-22 by the reprex package (v0.2.0).